【发布时间】:2011-05-01 17:50:10
【问题描述】:
我有以下问题:
我在 matplotlib.pyplot 中使用 hist()
我正在尝试在同一张图上创建 4 个直方图。以及它们中的每一个的近似高斯。
我怎样才能在同一张图上绘制 4 个直方图,而不会使它们相互阻塞(并排)?有什么想法吗?
【问题讨论】:
标签: python histogram matplotlib
我有以下问题:
我在 matplotlib.pyplot 中使用 hist()
我正在尝试在同一张图上创建 4 个直方图。以及它们中的每一个的近似高斯。
我怎样才能在同一张图上绘制 4 个直方图,而不会使它们相互阻塞(并排)?有什么想法吗?
【问题讨论】:
标签: python histogram matplotlib
matplotlib documentation 中有几个例子。这个好像回答了你的问题:
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()
【讨论】: