修改Matplotlib多色直方图脚本以使其累积

时间:2013-03-07 16:51:37

标签: python matplotlib plot histogram

我试图制作直方图,其中不同的条纹是不同的颜色。我很高兴找到this page提供脚本来为我做这一切。只有一个问题 - 它似乎不支持累积直方图,就像你可以通过在常规Matplotlib直方图中添加“cumulative = True”一样。 有人可以告诉我如何修改this file让我绘制累积的多色直方图吗? 非常感谢, 亚历

编辑:我认为最好的解决方案是在直方图类中添加一个函数,如下所示:

def make_cumulative(self):
  self.occurrences = np.cumsum(self.occurrences)

但是我不知道将哪些事件存储为,或者即使将其视为参数也是有意义的。

1 个答案:

答案 0 :(得分:2)

后见之明,解决方案非常简单明了。直方图对象只是每个bin的出现数组,所以我只取了直方图本身的cumsum。

所以基本上

import histogram, numpy 
y = range(0, 100) #Except I used real data
Hist = histogram(y, bins=100, range=[0,100])
colors = ['red', 'blue', 'green', ]
ranges = [[0,30], [30,31], [31,100]]
fig = pyplot.figure(figsize=(8,6))
ax, plt, _ = fig.plothist(Hist, alpha=0) # plot for spacing
for c, r in zip(colors, ranges):
    plt = ax.overlay(Hist, range=r, facecolor=c)
print y

CumulativeHist = numpy.cumsum(h6)
colors = ['red', 'blue', 'green', ]
ranges = [[0,30], [30,31], [31,100]]
fig = pyplot.figure(figsize=(8,6))
ax, plt, _ = fig.plothist(CumulativeHist, alpha=0) # plot for spacing
for c, r in zip(colors, ranges):
    plt = ax.overlay(CumulativeHist, range=r, facecolor=c)

pyplot.show()

将制作两个图,其中第二个图是第二个图的累积版。 谢谢你的帮助。

亚历