使用 matplotlib 在单个图表上绘制两个直方图 [英] Plot two histograms on single chart with matplotlib
本文介绍了使用 matplotlib 在单个图表上绘制两个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用文件中的数据创建了一个直方图,没有问题.现在我想在同一个直方图中叠加来自另一个文件的数据,所以我做这样的事情
I created a histogram plot using data from a file and no problem. Now I wanted to superpose data from another file in the same histogram, so I do something like this
n,bins,patchs = ax.hist(mydata1,100)
n,bins,patchs = ax.hist(mydata2,100)
但问题是对于每个区间,只有最高值的条出现,而其他的则被隐藏.我想知道如何用不同的颜色同时绘制两个直方图.
but the problem is that for each interval, only the bar with the highest value appears, and the other is hidden. I wonder how could I plot both histograms at the same time with different colors.
推荐答案
这里有一个工作示例:
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()
这篇关于使用 matplotlib 在单个图表上绘制两个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文