python - 如何在同一张图上绘制 4 个直方图

标签 python histogram matplotlib


我有以下问题:
我在 matplotlib.pyplot 中使用 hist()
我正在尝试在同一张图上创建 4 个直方图。以及它们中每一个的近似高斯分布。
如何在同一张图上绘制 4 个直方图,而不让它们相互阻塞(并排)?有什么想法吗?

最佳答案

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()

关于python - 如何在同一张图上绘制 4 个直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5850207/

相关文章:

将参数传递给 PostgreSQL 查询时出现 Python TypeError : not all arguments converted during string formatting,

python - 有人可以为我解释这个递归吗?

matlab - 在 Matlab 中并排绘制直方图

python - 具有多个图例条目的 Matplotlib 直方图

python - Networkx:可视化 MultiGraph 时重叠边

matplotlib - 直方图 bin 是左关闭还是右关闭?

python - 为什么只有一半的预期输出使用纹理坐标环绕模式,来自 SB OpenGL 的示例

python - 自动输入 Python 提示

python - 我的摄像头实时直方图

python - 使用观察权重绘制密度