【发布时间】:2023-03-08 08:27:01
【问题描述】:
我有来自不同年龄组的数据,我试图将其绘制为直方图。我已经对年龄组进行了分类。当我将图表绘制为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);
【问题讨论】:
-
请不要发布代码、数据或 Tracebacks 的图像。将其复制并粘贴为文本,然后将其格式化为代码(选择它并输入
ctrl-k)...Discourage screenshots of code and/or errors。 How to make good reproducible pandas examples -
我觉得你想绘制
barplot而不是histogram。你可以试试:Cedar.plot(x = 'Age', y = 'Cedar', kind = 'bar', logy = False, figsize = [15,10], fontsize = 15);
标签: python pandas dataframe matplotlib histogram