How to set y-axis for historgram in Python?

21,068

Solution 1

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.

Solution 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.

Share:
21,068
TheRealFakeNews
Author by

TheRealFakeNews

Updated on October 11, 2020

Comments

  • TheRealFakeNews
    TheRealFakeNews over 3 years

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