简体   繁体   中英

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:

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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