2015-10-16 61 views
0

有什么办法可以在matplotlib/pyplot中制作分层直方图吗?Matplotlib使用分层直方图

我已经找到了如何使用的透明度与alpha标签,但我不能找到一种方法,它叠放,

如。如果有两个具有共同Y轴图的数据集,则应该先绘制最小频率,以便在较大频率之上可以看到分层。

透明度无法正常工作,因为它会改变颜色,使其与密钥不匹配。

回答

1

我觉得你可以根据自己的身高设置单个条的z顺序得到你要寻找的效果:

import numpy as np 
from matplotlib import pyplot as plt 

# two overlapping distributions 
x1 = np.random.beta(2, 5, 500) 
x2 = np.random.beta(5, 2, 500) 

fig, ax = plt.subplots(1, 1) 
ax.hold(True) 

# plot the histograms as usual 
bins = np.linspace(0, 1, 20) 
counts1, edges1, bars1 = ax.hist(x1, bins) 
counts2, edges2, bars2 = ax.hist(x2, bins) 

# plot the histograms as lines as well for clarity 
ax.hist(x1, bins, histtype='step', ec='k', ls='solid', lw=3) 
ax.hist(x2, bins, histtype='step', ec='k', ls='dashed', lw=3) 

# set the z-order of each bar according to its relative height 
x2_bigger = counts2 > counts1 
for b1, b2, oo in zip(bars1, bars2, x2_bigger): 
    if oo: 
     # if bar 2 is taller than bar 1, place it behind bar 1 
     b2.set_zorder(b1.get_zorder() - 1) 
    else: 
     # otherwise place it in front 
     b2.set_zorder(b1.get_zorder() + 1) 

plt.show() 

enter image description here

+0

这工作,谢谢。 – rerpha