如何在同一图表上绘制4个直方图
我遇到了一个问题:
我在使用matplotlib.pyplot里的hist()函数。
我想在同一张图上画4个直方图,并且为每个直方图画一个高斯曲线(就是那种钟形曲线)。
请问我该怎么把这4个直方图放在同一张图上,而且不互相遮挡(并排放置)呢?有什么好主意吗?
1 个回答
10
在 matplotlib 的文档 中有几个例子。这个例子看起来可以解答你的问题:
import numpy as np
import pylab as P
#
# first create a single histogram
#
mu, sigma = 200, 25
x = mu + sigma*P.randn(10000)
#
# finally: make a multiple-histogram of data-sets with different length
#
x0 = mu + sigma*P.randn(10000)
x1 = mu + sigma*P.randn(7000)
x2 = mu + sigma*P.randn(3000)
# and exercise the weights option by arbitrarily giving the first half
# of each series only half the weight of the others:
w0 = np.ones_like(x0)
w0[:len(x0)/2] = 0.5
w1 = np.ones_like(x1)
w1[:len(x1)/2] = 0.5
w2 = np.ones_like(x2)
w0[:len(x2)/2] = 0.5
P.figure()
n, bins, patches = P.hist( [x0,x1,x2], 10, weights=[w0, w1, w2], histtype='bar')
P.show()