python - 在同一图上绘制多个直方图的问题

标签 python matplotlib histogram

我需要在同一个图上绘制多个直方图。我喜欢以下代码生成的显示:

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)
pyplot.hist(y, bins, alpha=0.5)
pyplot.show()

此页面上提到了此代码:Plot two histograms at the same time with matplotlib 基本上我在绘制相同类型的直方图时遇到了问题,但对于看起来像的数据:

y1=[20,33,54,34,22]
x1=[0,2,4,6,8]
y2=[28,31,59,14,12]
x2=[0,2,4,6,8]

使用上述代码我无法让 y 轴超过 2.0 奇怪但我一定是犯了一个愚蠢的错误。

谢谢。

最佳答案

可能您正在寻找这个:

 pyplot.bar(x2,y2, color='b', width=2, alpha=0.5)
 pyplot.bar(x1,y1, color='r', width=2, alpha=0.5)
 pyplot.show()

enter image description here

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

相关文章:

python - Tox 4 上的重用环境

Python/SQLAlchemy 迁移 - ValueError : need more than 3 values to unpack when migrating

python - PostgreSQL 不使用 psycopg2 在 AWS Lambda 上的 Python 中插入

matplotlib - Matplotlib错误: 'str' object has no attribute 'get_figure'

matplotlib - 无法使用 'map.drawcoaSTLine' 将 'ax.add_collection3d(map.drawcoaSTLines())' 添加到 3d 图形

r - 使用 ggplot geom_histogram() 与 y-log-scale 和零箱

plot - Stata 重叠直方图重叠处颜色不同

python - wxPython - Linux 和 Windows 之间的 TextCtrl 不同行为

python-3.x - Matplotlib 导入错误(需要 str、bytes 或 os.PathLike 对象,而不是 PosixPath)

rotation - matplotlib:直方图和bin标签