[英]how to plot a histogram from a dataframe
我有来自不同年龄组的数据,我试图将其绘制为直方图。 我已经对年龄组进行了分类。 当我将图形绘制为bar
或line
图时,我的数据看起来不错,但是当我尝试将图形绘制为histogram
,图形是错误的。 我究竟做错了什么?
分档和保存:
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')
绘制图形:
Cedar = pd.read_csv('Cedar.csv')
plt.figure();
Cedar.plot(x = 'Age',
y = 'Cedar',
kind = 'hist',
logy = False,
figsize = [15,10],
fontsize = 15);
你做错的是你正在对数据进行分箱。 对于直方图,您可以提供纯数据并将 bin 设置为参数。
你所做的就是制作雪松值的直方图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.