如何绘制彼此相邻的条形图?

时间:2021-01-14 09:52:57

标签: python matplotlib histogram

我有一个直方图,每个 bin 上有 4 个不同的对象,现在它们彼此堆叠在一起。相反,我需要在同一个直方图 bin 内并排绘制不同的对象(类似于 https://matplotlib.org/3.1.1/gallery/statistics/histogram_multihist.html 中的左上图):

bins=np.logspace(np.log10(0.01),np.log10(20), 11)

plt.hist(a[nosfr]/1e+11, bins, color='red', fill=True, linewidth=2, density=True, histtype='bar', edgecolor='k')

plt.hist(a[highsfr]/1e+11, bins, color='orange', fill=True, linewidth=2, density=True, histtype='bar', edgecolor='k')

plt.hist(b[mynosfr]/1e+11, bins, color='blue', edgecolor='k', fill=True, linewidth=2, density=True, alpha=0.7, histtype='bar')

plt.hist(b[myhighsfr]/1e+11, bins, color='cyan', edgecolor='k', fill=True, linewidth=2, density=True, alpha=0.7, histtype='bar')

plt.xscale('log')
plt.xlim(2e-2, 2e+1)

[nosfr][highsfr] 等在同一样本(ab)中使用不同的标准绘制对象。我看过的所有例子都与我需要的略有不同,我找不到正确的方法。谢谢!

1 个答案:

答案 0 :(得分:0)

从您的数据框中调用 plot 方法,并将 kind 参数设置为 bar。

x = np.random.random((10, 4))
df = pd.DataFrame(x, columns=['a', 'b', 'c', 'd'])
df.plot(kind='bar')

结果如下:
Bar plot