python里画堆叠直方图_如何在matplotlib中并排绘制堆积直方图?

图中显示的是bar chart,而不是histogram。我指出这一点,不仅因为我是一个讨厌的学究,而且我相信它可以帮助你找到正确的工具:-)

实际上,就您的目的而言,plt.bar可能比plt.hist更好。在

根据Scironic的建议,我修改了这个demonstration example来制作堆叠的条形图,就像你的图中的那样。在

向位置索引添加偏移量(在plt.bar()中的第一个参数)可以防止这些条相互重叠。在import numpy as np

import matplotlib.pyplot as plt

N = 5

men1 = (130, 90, 70, 64, 55)

men2 = (120, 85, 62, 50, 53)

men3 = (100, 70, 60, 45, 50)

ind = np.arange(N) + .15 # the x locations for the groups

width = 0.35 # the width of the bars

fig, ax = plt.subplots()

rects1 = ax.bar(ind, men1, width, color='g')

rects2 = ax.bar(ind, men2, width, color='r')

rects3 = ax.bar(ind, men3, width, color='b')

women4 = (140, 90, 78, 65, 50)

women5 = (130, 80, 70, 60, 45)

women6 = (120, 60, 60, 55, 44)

xtra_space = 0.05

rects2 = ax.bar(ind + width + xtra_space , women1, width, color='orange')

rects2 = ax.bar(ind + width + xtra_space, women2, width, color='cyan')

rects2 = ax.bar(ind + width + xtra_space, women3, width, color='purple')

# add some text for labels, title and axes ticks

ax.set_ylabel('Population, millions')

ax.set_title('Population: Age Structure')

ax.set_xticks(ind+width+xtra_space)

ax.set_xticklabels( ('USA', 'Brazil', 'Russia', 'Japan', 'Mexico') )

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值