5

According to the documentation, one can set the range of the x-axis using the hist function, but there doesn't seem to be a way to control the y-axis.

I have a figure with 4 subplots, arranged in a 2x2 fashion, all of which are histograms. I have made their x-axis to be entirely the same by setting the range, but have been unable to figure out how to do likewise with the y-axis. But when I try to control the y-axis, using set_ylim, I get an error. When I tried using pylab.axis, the plots didn't turn out correctly (the bars of the historgram all had a y-value of 0.

pylab.hist(myData[x], bins = 20, range=(0,400))
pylab.axis([0,400,0,300])

How do I control the y-axis of the histogram? Essentially what I"m looking for is something like range in the hist function, but for the y-axis.


Update:

    plotNumber = 1
for i in xrange(4):
    pylab.subplot(2, 2, plotNumber)
    pylab.hist(myData[i], bins = 20, range=(0,400))
    pylab.title('Some Title')
    pylab.xlabel('X')
    pylab.ylabel('Y')
    plotNumber += 1
pylab.show()

But when I include

pylab.axis([0,400,0,300])

All the y-values correspond to 0 (the histogram is flat).

TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
  • I tried your example (using random data, but the same histogram parameters) and was able to set both x and y limits using xlim() and ylim(). What's the text of the error you get? – Derek T. Jones Jun 21 '15 at 05:24
  • @DerekJones I don't get an error, I just get a histogram that doesn't make sense. – TheRealFakeNews Jun 23 '15 at 08:22

2 Answers2

4

Answer is given here: setting y-axis limit in matplotlib

axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])

For me this works for histogram subplots.

Community
  • 1
  • 1
Niels Hameleers
  • 1,201
  • 10
  • 11
2

If you're looking to set ticks on the y-axis every n values, you can use:

pylab.yticks(range(min, max, n))

I am using Python 2.7.