简体   繁体   English

使用matplotlib,如何通过覆盖直方图并显示其比率图来比较直方图?

[英]Using matplotlib, how could one compare histograms by overlaying them and by showing their ratio plot?

Two histograms can be compared by creating a plot that features both an overlay of the histograms (possibly normalised) and a ratio plot of the histograms. 可以通过创建一个以直方图的覆盖图(可能是归一化的)和直方图的比率图为特征的图来比较两个直方图。 Here is such a plot: 这是这样的情节:

How could a plot like this be made using matplotlib? 使用matplotlib如何制作这样的图?

I don't see what the dots are, but here's a simple example of the ratios. 我看不到点是什么,但这是比率的一个简单示例。 The main trick is to reuse the bin values that hist returns. 主要技巧是重用hist返回的bin值。

在此处输入图片说明

import matplotlib.pyplot as plt
from numpy.random import normal

y = []
y.append(normal(2, 2, size=120))
y.append(normal(2, 2, size=120))

fig, (ax1, ax2) = plt.subplots(nrows=2)

ns, bins, patches = ax1.hist(y, normed=False,
                      histtype='stepfilled',
                      bins=8,
                      alpha=0.2,
                      label=['a','b']
                      )
ax1.legend()

ax2.bar(bins[:-1],     # this is what makes it comparable
        ns[0] / ns[1], # maybe check for div-by-zero!
        alpha=0.4)

ax1.set_ylabel('Data')
ax2.set_ylabel('Ratio (a/b)')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM