【问题标题】:Python altering my plot data points? [histogram]Python 改变我的绘图数据点? [直方图]
【发布时间】:2020-06-07 02:01:27
【问题描述】:

尝试从 Excel 工作表中提取数据,应用等式(在本例中为 median()),并根据该数据创建直方图。

这是我的代码:

import pandas as pd
import matplotlib.pyplot as plt

pd.set_option('display.max_columns', 100000)
absent = pd.read_excel('Absenteeism_at_work.xls')
col = ['Distance from Residence to Work', 'Transportation expense', 'Month of absence', 'Social smoker',
       'Social drinker', 'Education']

# print(absent.loc[:741, col])

plt.title('The Mean')
plt.xlabel('Attribute of Absence')
plt.ylabel('Value')
# x = ['Distance', 'Trans Exp.', 'Month', 'Smoker', 'Drinker', 'Edu.']
x = absent.loc[:741, col].median()
x.plot(kind="bar", figsize=(5, 5))
print(x)
plt.hist(x)

# print(hist)
plt.show() # shows histogram in side-window

这是终端输出:

Distance from Residence to Work     26.0
Transportation expense             225.0
Month of absence                     6.0
Social smoker                        0.0
Social drinker                       1.0
Education                            1.0
dtype: float64

最重要的是,直方图不正确:

“社交吸烟者”不应该显示为 0 吗?另外,“从住所到工作的距离”右侧的那个额外的栏是什么?这合适吗?谢谢!

【问题讨论】:

    标签: python excel pandas matplotlib histogram


    【解决方案1】:

    您的图表x.plot(kind="bar", figsize=(5, 5))plt.hist(x) 合并。

    x.plot(kind="bar", figsize=(5, 5)):

    plt.hist(x):

    综合:

    【讨论】:

    • 不相关,但您知道如何以大致相同的方式创建散点图吗?
    • plt.scatter(X, y)
    • 但是散点图需要两个数值变量。您必须将标签更改为某种数字形式。
    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多