参考此链接中显示为答案的条形图
python matplotlib multiple bars
我想在蓝色条内有绿色条,这些条都在红色条内。是的,不应该堆叠,而是每个条的宽度应该不同。
有没有人能帮我解决一些线索。感谢。
答案 0 :(得分:1)
使用您引用的示例,您可以嵌套不同宽度的条,如下所示。请注意,如果条形的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()