簡體   English   中英

如何在matplotlib中繪制具有相同分檔寬度的直方圖,以獲得不等間距的分檔

[英]How to draw histogram with same bins width for unequally spaced bins in matplotlib

我試圖在matplotlib中繪制一個包含多個數據系列的直方圖。

我有不等間隔的箱子,但是我希望每個箱子都有相同的寬度。 所以我用這種方式使用了屬性width

aa = [0,1,1,2,3,3,4,4,4,4,5,6,7,9]
plt.hist([aa, aa], bins=[0,3,9], width=0.2)

結果是這樣的:

具有不等間隔箱的直方圖

如何擺脫兩個系列中兩個代號箱之間的余量? 即我怎樣才能為每個垃圾箱分組不同系列的酒吧?

謝謝

一個解決方案是通過numpy計算直方圖並手動單獨繪制條形圖:

aa1 = [0,1,1,2,3,3,4,4,5,9]
aa2 = [0,1,3,3,4,4,4,4,5,6,7,9]
bins = [0,3,9]
height = [np.histogram( xs, bins=bins)[0] for xs in [aa1, aa2]]
left, n = np.arange(len(bins)-1), len(height)

ax = plt.subplot(111)
color_cycle = ax._get_lines.color_cycle

for j, h in enumerate(height):
    ax.bar(left + j / n, h, width=1.0/n, color=next(color_cycle))

ax.set_xticks(np.arange(0, len(bins)))
ax.set_xticklabels(map(str, bins))

HIST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM