如何使用 matplotlib.pyplot 在另一个中制作多个条形图 [英] how to make multiple bar plots one within another using matplotlib.pyplot

查看:34
本文介绍了如何使用 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 (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()

这篇关于如何使用 matplotlib.pyplot 在另一个中制作多个条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆