如何从数据框中绘制直方图
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 设置为参数。
您所做的是制作雪松值的直方图。
我有来自不同年龄组的数据,我试图将其绘制为直方图。我把年龄组分类了。当我将图表绘制为 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 设置为参数。
您所做的是制作雪松值的直方图。