import random
import numpy
from matplotlib import pyplot
x = [random.gauss(3,1) for _ in range(400)]
y = [random.gauss(4,2) for _ in range(400)]
bins = numpy.linspace(-10, 10, 100)
pyplot.hist(x, bins, alpha=0.5, label='x')
pyplot.hist(y, bins, alpha=0.5, label='y')
pyplot.legend(loc='upper right')
pyplot.show()
接受的答案给出了带有重叠条的直方图代码,但如果您希望每个条并排(如我所做的),请尝试以下变化:
参考:http://matplotlib.org/examples/statistics/histogram_demo_multihist.html
编辑[2018/03/16]:根据@Randomic_zeitgeist的建议,更新以允许绘制不同尺寸的阵列
如果样本大小不同,可能很难用单个y轴比较分布。例如:
在这种情况下,可以在不同的轴上绘制两个数据集。为此,可以使用matplotlib获取直方图数据,清除轴,然后在两个单独的轴上重新绘制(移动箱子边缘,使其不重叠):
这里有一个工作示例:
相关问题 更多 >
编程相关推荐