我说两种分布具有不同的价差
a=N.random.normal(0,0.5,500)
b=N.random.normal(1,3.,500)
我想对它们进行直方图,其中最大值处于同一水平。
如果我使用density = True参数制作归一化的直方图,它将使两个直方图的面积均为1,这不会改变最大值不同的事实。
P.hist(a, histtype='step', lw=2, cumulative=True)
P.hist(b, histtype='step', color='r', lw=2, density=True)
我想要的是制作一个以最大值“标准化”的直方图。我的意思是当蓝色和红色直方图的最大值相同时,因此比较它们可能很容易。在此先感谢您的帮助。
我不确定我是否真的会这样做,但是如果您想,我认为最好的方法是添加两个轴(也可以看到它们的实际高度)。例如,请参见此处:https : //matplotlib.org/gallery/api/two_scales.html
import numpy as np
from matplotlib import pyplot as plt
fig, ax1 = plt.subplots()
ax1.hist(a, histtype='step', color='b', lw=2, density=True)
ax1.tick_params(axis='y', labelcolor='b')
ax2 = ax1.twinx()
ax2.hist(b, histtype='step', color='r', lw=2, density=True)
ax2.tick_params(axis='y', labelcolor='r')
这给出了以下输出(我认为,它看起来比获得的结果差;在第一张图中,我也将cumulative = True更改为density = True,使其与您提供的图一致):
同样,严格来讲,这不能确保最大值确实相同。如果您想这样做,可以通过执行例如
import numpy as np
from matplotlib import pyplot as plt
fig, ax1 = plt.subplots()
n1, _, _ = ax1.hist(a, histtype='step', color='b', lw=2, density=True)
ax1.tick_params(axis='y', labelcolor='b')
ax2 = ax1.twinx()
n2, _, _ = ax2.hist(b, histtype='step', color='r', lw=2, density=True)
ax2.tick_params(axis='y', labelcolor='r')
ax1.set_ylim([0, n1.max()*1.1])
ax2.set_ylim([0, n2.max()*1.1])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句