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
Solution 1:
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 (i.e., 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()
Post a Comment for "How To Make Multiple Bar Plots One Within Another Using Matplotlib.pyplot"