繁体   English   中英

如何使用matplotlib.pyplot在另一个内部制作多个条形图

[英]how to make multiple bar plots one within another using matplotlib.pyplot

参考此链接中显示为答案的条形图

python matplotlib多个栏

我想在蓝色栏中包含绿色栏,而在红色栏中包含这两个栏。 是的,不应将其堆叠,而每个条应具有不同的宽度。

谁能让我开始了解一些线索。 谢谢。

使用您引用的示例,可以嵌套不同宽度的条,如下所示。 请注意,如果一个条的y值较小,则它只能“包含”在另一个条中(即,请参见下图中的第三组条)。 基本思想是为条形图设置fill = False ,以使它们不会相互遮挡。 您也可以尝试制作具有半透明(低alpha )填充色的条形,但这往往会造成混乱,尤其是红色,蓝色和绿色叠加在一起时。

import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib.dates import date2num
import datetime

x = [datetime.datetime(2011, 1, 4, 0, 0),
     datetime.datetime(2011, 1, 5, 0, 0),
     datetime.datetime(2011, 1, 6, 0, 0)]
x = date2num(x)

y = [4, 9, 2]
z=[1,2,3]
k=[11,12,13]

ax = plt.subplot(111)

#first strategy is to use hollow bars with fill=False so that they can be reasonably superposed / contained within one another:
ax.bar(x, z,width=0.2,edgecolor='g',align='center', fill=False) #the green bar has the smallest width as it is contained within the other two
ax.bar(x, y,width=0.3,edgecolor='b',align='center', fill=False) #the blue bar has a greater width than the green bar
ax.bar(x, k,width=0.4,edgecolor='r',align='center', fill=False) #the widest bar encompasses the other two
ax.xaxis_date()

plt.show()

在此处输入图片说明

暂无
暂无

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

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