【问题标题】:how to plot a histogram from a dataframe如何从数据框中绘制直方图
【发布时间】:2023-03-08 08:27:01
【问题描述】:

我有来自不同年龄组的数据,我试图将其绘制为直方图。我已经对年龄组进行了分类。当我将图表绘制为barline 图表时,我的数据看起来不错,但是当我尝试绘制为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);

【问题讨论】:

标签: python pandas dataframe matplotlib histogram


【解决方案1】:

您做错的是您正在对数据进行分箱。对于直方图,您可以提供纯数据并将 bin 设置为参数。

您正在制作雪松值的直方图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2021-05-20
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多