python 并列条形图_如何在python中使用matplotlib绘制叠加条形图?

在@Ffisegydd的answer基础上构建,如果您的数据位于Pandas数据帧中,那么这应该可以很好地工作:def overlapped_bar(df, show=False, width=0.9, alpha=.5,

title='', xlabel='', ylabel='', **plot_kwargs):

"""Like a stacked bar chart except bars on top of each other with transparency"""

xlabel = xlabel or df.index.name

N = len(df)

M = len(df.columns)

indices = np.arange(N)

colors = ['steelblue', 'firebrick', 'darksage', 'goldenrod', 'gray'] * int(M / 5. + 1)

for i, label, color in zip(range(M), df.columns, colors):

kwargs = plot_kwargs

kwargs.update({'color': color, 'label': label})

plt.bar(indices, df[label], width=width, alpha=alpha if i else 1, **kwargs)

plt.xticks(indices + .5 * width,

['{}'.format(idx) for idx in df.index.values])

plt.legend()

plt.title(title)

plt.xlabel(xlabel)

plt.ylabel(ylabel)

if show:

plt.show()

return plt.gcf()

然后在python命令行中:low = [1000.95, 1233.37, 1198.97, 1198.01, 1214.29, 1130.86, 1138.70, 1104.12, 1012.95, 1000.36]

high = [1184.53, 1523.48, 1521.05, 1517.88, 1519.88, 1414.98, 1419.34, 1415.13, 1182.70, 1165.17]

df = pd.DataFrame(np.matrix([high, low]).T, columns=['High', 'Low'],

index=pd.Index(['T%s' %i for i in range(len(high))],

name='Index'))

overlapped_bar(df, show=False)

rLe9K.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值