简体   繁体   English

如何从数据框中绘制直方图

[英]how to plot a histogram from a dataframe

I have data from different age groups that I am trying to plot as a histogram.我有来自不同年龄组的数据,我试图将其绘制为直方图。 I have binned the age groups.我已经对年龄组进行了分类。 When I plot my graph as a bar or line graph my data looks good but when I try to plot as a histogram the graph is wrong.当我将图形绘制为barline图时,我的数据看起来不错,但是当我尝试将图形绘制为histogram ,图形是错误的。 What am I doing wrong?我究竟做错了什么?

Binning and saving:分档和保存:

df = pd.read_csv('test.csv')
age_groups = pd.cut(df['Age'], bins=[0, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,np.inf])
Cedar = df.groupby(age_groups)['Cedar'].mean()
Cedar = pd.DataFrame(Cedar, index = None)
Cedar.to_csv('Cedar.csv')

plotting the graph:绘制图形:

Cedar = pd.read_csv('Cedar.csv')
plt.figure();
Cedar.plot(x = 'Age', 
           y = 'Cedar', 
           kind = 'hist', 
           logy = False, 
           figsize = [15,10], 
           fontsize = 15);

What you are doing wrong is that you are binning the data.你做错的是你正在对数据进行分箱。 For histogram plots you can give the pure data and set the bins as parameter.对于直方图,您可以提供纯数据并将 bin 设置为参数。

The way you did you are making an histogram of the cedar values.你所做的就是制作雪松值的直方图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM