简体   繁体   English

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

[英]Issue with plotting multiple histograms on the same plot

I need to plot several histograms on the same plot. 我需要在同一图上绘制几个直方图。 I like the display the following code generates: 我喜欢以下代码生成的显示:

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

This code was mentioned on this page: Plot two histograms at the same time with matplotlib Basically I am having trouble plotting the same kind of histograms but for data that looks like: 本页中提到了这段代码: 使用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]

Using the aforementioned code I could not get the y axis to go above 2.0 strange but I must be making a foolish mistake. 使用前面提到的代码,我无法使y轴超过2.0,但我一定犯了一个愚蠢的错误。

Thanks. 谢谢。

Probably you are looking for this: 可能您正在寻找:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM