带有数据帧的Python熊猫直方图

问题描述 投票:0回答:2

我正在尝试创建一个直方图来表示数据框的多个元素。

数据帧如下:

        Freq
Item 1   25
Item 2   35

[我正在尝试使用底部沿第1项和第2项的直方图,并将代表高度(在y轴上)的数字作为频率(注意,我不想累积频率)。

我已经尝试过:

dataframe = pd.DataFrame.from_dict(tour_adjust, orient='index')
plt.hist(dataframe, bins=10)
plt.show()

并且我得到这样的结果:Image showing the histogram

((实际结果仅与期望值不同,具体取决于数据框中的元素数)

与期望的结果相反,请参见此处:

Image showing the desired result

我尝试过使用orient ='index'并没有它,但是两者都不正确,不是我想要的。

python pandas dataframe histogram
2个回答
1
投票

如果您的数字已经是频率,那么您要找的不是hist图,而是bar图?

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.bar.html

更新

您还可以直接从数据框进行绘制,例如在this example中:

>>> df = pd.DataFrame({'lab':['A', 'B', 'C'], 'val':[10, 30, 20]})
>>> ax = df.plot.bar(x='lab', y='val', rot=0)

1
投票

我认为您需要:

df.plot(kind = 'bar')

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.