[英]Histogram bars cannot stacked using matplotlib in python
我在 python 中使用“matplotlib”實現了直方圖 plot。我有兩個變量 x1 和 x2,每個變量有 15 個元素。 運行代碼時,直方圖條不是堆疊而是重疊,如下圖所示。
我想要 plot 變量條形的堆疊直方圖。
這是代碼:
x1= [23, 25, 40, 35, 40, 53, 33, 28, 55, 34, 20, 37, 36, 23, 33]
x2= [36, 20, 27, 50, 34, 47, 18, 28, 52, 21, 44, 34, 13, 40, 49]
colors = ['blue', 'orange']
bins = [10,20,30,40,50,60]
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)
ax0.hist(x1,bins = bins, histtype='bar', label=colors[0], rwidth=0.8)
ax0.hist(x2,bins, histtype='bar', stacked=True, label=colors[1], rwidth=0.8)
ax1.hist(x1, bins = bins, histtype='bar', label=colors[0], rwidth=0.8)
ax1.hist(x2,bins = bins, histtype='bar', stacked=True, label=colors[1], rwidth=0.8)
ax2.hist(x1, bins = bins, histtype='bar', label=colors[0], rwidth=0.8)
ax2.hist(x2,bins = bins, histtype='bar', stacked=True, label=colors[1], rwidth=0.8)
plt.show()
嘗試將兩個列表一起傳遞並使用stacked=True
。 只傳遞一個列表並使用stacked=True
沒有多大意義。
ax0.hist([x1, x2], bins, histtype='bar', stacked=True, label=colors, rwidth=0.8)
ax1.hist([x1, x2], bins, histtype='bar', stacked=True, label=colors, rwidth=0.8)
ax2.hist([x1, x2], bins, histtype='bar', stacked=True, label=colors, rwidth=0.8)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.