2017-09-06 48 views
0

爲什麼第二個系列不總是顯示出來,如下所示?Matplotlib堆積條形圖系列不顯示


方案1:

score_sim = (70, 80, 90) 
score_feat = (10, 20, 30) 
ind = np.arange(len(score_sim)) 
width = 0.35 

p1 = plt.bar(ind, score_feat, width) 
p2 = plt.bar(ind, score_sim, width) 

給出:enter image description here

P2沒有顯示?


方案2:

score_sim = (70, 80, 90) 
score_feat = (100, 200, 300) 
ind = np.arange(len(score_sim)) 
width = 0.35 

p1 = plt.bar(ind, score_feat, width) 
p2 = plt.bar(ind, score_sim, width) 

得到:enter image description here



那麼,爲什麼只有第二個表演兩個系列? 我想要綠色以下的藍色系列。我該怎麼做呢?

回答

1

在情況1中,第二個圖的條形大於第一個圖形的條形。因此他們覆蓋其他酒吧。在情景2中,第二個小節的小節比第一個小節的小小,因此背景中的小節仍然可見。

請注意,這兩個圖不顯示「堆疊」的酒吧;所有小節從y = 0開始。

爲了在其他的最簡單的解決方案前所示的具體情節是情節是最後的,即在方案比1

p2 = plt.bar(ind, score_sim, width) 
p1 = plt.bar(ind, score_feat, width) 

其他,你可以使用zorder繪製一個情節另一個的前面。情節越高,zorder越高。

p1 = plt.bar(ind, score_feat, width, zorder=4) 
p2 = plt.bar(ind, score_sim, width, zorder=3) 
# p1 will be shown in front of p2, even though it is later defined, 
# because it has the larger zorder 
+0

哦好吧,謝謝。 我沒有意識到這些圖都是從零開始並被覆蓋的。 – Mierzen

1

在方案1中,score_feat條正確繪製,但隨後他們由值score_sim覆蓋。 plt.bar的參數bottom接受標量或數組並指定一個條的垂直起點。例如如果要在場景1上堆疊兩個系列的條形圖,請將其用作第二個繪圖命令:

p2 = plt.bar(ind, score_sim, width,bottom=score_feat)