Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python改变我的绘图数据点?[直方图]_Python_Excel_Pandas_Matplotlib_Histogram - Fatal编程技术网

Python改变我的绘图数据点?[直方图]

Python改变我的绘图数据点?[直方图],python,excel,pandas,matplotlib,histogram,Python,Excel,Pandas,Matplotlib,Histogram,尝试从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', 'T

尝试从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
最重要的是,直方图不正确:

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吗?另外,“从住宅到工作的距离”右侧的额外一点是什么?这样合适吗?谢谢大家!

您的图形
x.plot(kind=“bar”,figsize=(5,5))
plt.hist(x)
合并

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

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

plt.hist(x):

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

组合:

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

不相关,但您知道如何以大致相同的方式创建散点图吗?plt.散点图(X,y)但散点图您需要两个数值变量。你必须把标签改成数字形式。