简体   繁体   中英

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

With reference to the bar chart shown as answer in this link

python matplotlib multiple bars

I would like to have green bar inside blue bar and both these bars inside red bar. And yes it should not be stacked rather each of the bars should be of different width.

Could anyone get me started with some clue. Thanks.

Using the example you reference, you can nest the bars with different widths as shown below. Note that a bar can only be 'contained' within another bar if its y value is smaller (ie, see the third set of bars in the plot below). The basic idea is to set fill = False for the bars so that they don't obscure one another. You could also try making bars with semi-transparent (low alpha ) fill colours, but this tends to get pretty confusing--especially with red, blue, and green all superposed.

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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