Reputation: 1241
I have two arrays:
import numpy as np
import pylab as pl
x = np.array([-36., -34.95522388, -33.91044776, -32.86567164,
-31.82089552, -30.7761194 , -29.73134328, -28.68656716,
-27.64179104, -26.59701493, -25.55223881, -24.50746269,
-23.46268657, -22.41791045, -21.37313433, -20.32835821,
-19.28358209, -18.23880597, -17.19402985, -16.14925373,
-15.10447761, -14.05970149, -13.01492537, -11.97014925,
-10.92537313, -9.88059701, -8.8358209 , -7.79104478,
-6.74626866, -5.70149254, -4.65671642, -3.6119403 ,
-2.56716418, -1.52238806, -0.47761194, 0.56716418,
1.6119403 , 2.65671642, 3.70149254, 4.74626866,
5.79104478, 6.8358209 , 7.88059701, 8.92537313,
9.97014925, 11.01492537, 12.05970149, 13.10447761,
14.14925373, 15.19402985, 16.23880597, 17.28358209,
18.32835821, 19.37313433, 20.41791045, 21.46268657,
22.50746269, 23.55223881, 24.59701493, 25.64179104,
26.68656716, 27.73134328, 28.7761194 , 29.82089552,
30.86567164, 31.91044776, 32.95522388, 34. ])
y = np.array([ 28, 25, 30, 20, 32, 20, 10, 20, 9, 18, 10, 7, 7,
14, 10, 11, 4, 8, 7, 11, 3, 7, 3, 1, 4, 3,
1, 5, 1, 4, 1, 1, 1, 55, 2, 6, 2, 2, 5,
5, 5, 10, 10, 17, 26, 28, 30, 34, 103, 137, 84, 59,
55, 69, 59, 70, 72, 75, 66, 90, 79, 74, 62, 80, 59,
62, 36, 43])
Both x
and y
have the same size. Now I want to plot a Histogram, where x
represents the x axis and y
the y axis. I try the following code:
pl.hist(y,x)
The resulting image is this one:
In this plot the maximum value goes up to seven, which does not make sense, since on the y
array there are values as high as 137. The x
array seems to be working, but I cannot figure out what is wrong with my y
array.
I was following this example here:
Plot two histograms at the same time with matplotlib
Upvotes: 1
Views: 7243
Reputation: 968
You are using the wrong function. You should be using pl.bar()
as in http://matplotlib.org/examples/api/barchart_demo.html
What hist()
does is take counts of the data in your vector and then plots bars of those counts. For example, if you have x=[1 1 3 2 5 5 5 2]
, then hist(x)
will give a bar graph with height 2 at position 1, height 2 at position 2, height 1 at postion 3, height 0 at position 4 and height 3 at position 5.
Upvotes: 8
Reputation: 20695
Your data is already "binned", so-to-speak. plt.hist
takes unbinned data, bins it, and plots a histogram. You simply need plt.bar
:
>>> plt.bar(x, y)
Which gives:
Upvotes: 9