【发布时间】:2015-11-23 16:08:27
【问题描述】:
参考此链接中作为答案显示的条形图
python matplotlib multiple bars
我想在蓝条里面有绿条,在红条里面有这两个条。是的,它不应该堆叠,而是每个条的宽度应该不同。
谁能让我从一些线索开始。谢谢。
【问题讨论】:
标签: python matplotlib
参考此链接中作为答案显示的条形图
python matplotlib multiple bars
我想在蓝条里面有绿条,在红条里面有这两个条。是的,它不应该堆叠,而是每个条的宽度应该不同。
谁能让我从一些线索开始。谢谢。
【问题讨论】:
标签: 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()
【讨论】: