我试图用matplotlib绘制多个数据序列的直方图。
我有不一样间距的垃圾箱,但是我希望每个垃圾箱都有相同的宽度。因此,我以这样的方式使用属性width
:
aa = [0,1,1,2,3,3,4,4,4,4,5,6,7,9]
plt.hist([aa, aa], bins=[0,3,9], width=0.2)
其结果是:
我怎样才能摆脱这两个系列的两个通讯员之间的差距?也就是说,我如何为每个垃圾箱分组不同系列的酒吧?
谢谢
发布于 2014-03-17 10:02:16
解决方案可以是用numpy计算直方图,然后手工绘制条形图:
aa1 = [0,1,1,2,3,3,4,4,5,9]
aa2 = [0,1,3,3,4,4,4,4,5,6,7,9]
bins = [0,3,9]
height = [np.histogram( xs, bins=bins)[0] for xs in [aa1, aa2]]
left, n = np.arange(len(bins)-1), len(height)
ax = plt.subplot(111)
color_cycle = ax._get_lines.color_cycle
for j, h in enumerate(height):
ax.bar(left + j / n, h, width=1.0/n, color=next(color_cycle))
ax.set_xticks(np.arange(0, len(bins)))
ax.set_xticklabels(map(str, bins))
https://stackoverflow.com/questions/22461355
复制相似问题