简体   繁体   English

matplotlib中水平堆积条形图的问题

[英]Problem in horizontal stacked bar graph in matplotlib

Below is my code which creates horizontal stacked bar graph:下面是我创建水平堆积条形图的代码:

import matplotlib.pyplot as plt

year = [2014]  
tutorial_public = [30]  
tutorial_premium = [10]
tutorial_premiumx = [20] 

fig, axs = plt.subplots(1)

axs.barh(year, tutorial_premium, color="yellow")  
axs.barh(year, tutorial_public, left=tutorial_premium, color="red")
axs.barh(year, tutorial_premiumx, left=tutorial_public, color="blue")

It produces the below image :它产生以下图像: 在此处输入图片说明

What i find absurd here is that length of the red part is only 20 but it should be 30 because tutorial_public = [30] .我在这里觉得荒谬的是,红色部分的长度只有 20,但它应该是 30,因为tutorial_public = [30] What am i doing wrong here?我在这里做错了什么?

The width of the red bar is 30, your problem is that you have hidden part of the bar with the blue bar (try it by commenting the last line of your code)红色条的宽度30,您的问题是您用蓝色条隐藏了部分条(通过注释代码的最后一行来尝试)

You need to adjust the left= argument of your third barh (Notice that I have converted your list to numpy arrays to facilitate arithmetic operations):您需要调整第三个barhleft=参数(请注意,我已将您的列表转换为 numpy 数组以方便算术运算):

import matplotlib.pyplot as plt

year = [2014]  
tutorial_public = np.array([30])
tutorial_premium = np.array([10])
tutorial_premiumx = np.array([20])

fig, axs = plt.subplots(1)

axs.barh(year, tutorial_premium, color="yellow")  
axs.barh(year, tutorial_public, left=tutorial_premium, color="red")
axs.barh(year, tutorial_premiumx, left=tutorial_premium+tutorial_public, color="blue")

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

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