简体   繁体   English

Matplotlib堆积的条形图系列未显示

[英]Matplotlib stacked bar chart series not showing

Why does the second series not always show up, as below? 为什么第二个系列不总是显示如下?


Scenario 1: 方案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)

Gives: 得到: 在此处输入图片说明

p2 isn't showing? p2没有显示?


Scenario 2: 方案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)

Gives: 得到: 在此处输入图片说明



So, why does only the second one show two series? 那么,为什么只有第二个显示两个系列呢? I want to have the blue series below the green one. 我想要蓝色系列低于绿色系列。 How do I do this? 我该怎么做呢?

In scenario 1, the bars for score_feat are correctly plotted, but then they are covered by the values for score_sim . 在方案1中,正确绘制了score_feat的小节,但随后它们被score_sim的值覆盖。 plt.bar has an argument bottom that accept a scalar or an array and specifies the vertical starting point of a bar. plt.bar的参数bottom接受标量或数组,并指定条的垂直起点。 eg If you want to stack the bars for the two series on scenario 1, use as second plotting command: 例如,如果要在场景1上堆叠两个系列的钢筋,请使用第二条绘图命令:

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

In Scenario 1, the bars of the second plot are larger than the bars from the first plot. 在方案1中,第二个图的条形图大于第一个图的条形图。 Hence they overlay the other bars. 因此,它们覆盖了其他条。 In scenario 2, the bars of the second plot are smaller than those from the first, hence the bars in the background are still visible. 在方案2中,第二个图的条形小于第一个图的条形,因此背景中的条仍然可见。

Note that both plots do not show "stacked" bars; 请注意,两个图均未显示“堆积”的条形。 all bars start at y=0. 所有条形都从y = 0开始。

In order to have a specific plot shown in front of the other the easiest solution is to plot is last, ie in scenario 1 为了使特定的图显示在另一个图的前面,最简单的解决方案是将图绘制在最后,即在场景1中

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

Other than that, you may use zorder to plot one plot in front of the other. 除此之外,您可以使用zorder在另一个图上绘制一个图。 The higher the zorder to more in front the plot. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM