python – setting y-axis limit in matplotlib

The Question :

486 people think this question is useful

I need help with setting the limits of y-axis on matplotlib. Here is the code that I tried, unsuccessfully.

import matplotlib.pyplot as plt

plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')
ax = []
aPlot = plt.subplot(321, axisbg = 'w', title = "Year 1")
ax.append(aPlot)
plt.plot(paramValues,plotDataPrice[0], color = '#340B8C', 
     marker = 'o', ms = 5, mfc = '#EB1717')
plt.xticks(paramValues)
plt.ylabel('Average Price')
plt.xlabel('Mark-up')
plt.grid(True)
plt.ylim((25,250))

With the data I have for this plot, I get y-axis limits of 20 and 200. However, I want the limits 20 and 250.

The Question Comments :
  • Works for me with Matplotlib 1.0.0 if I add plt.show() at the end to show the plot. Which version and which backend are you using?
  • Working for me with Matplotlib 0.98.5.2, Python 2.6.2. I tried both plt.ylim((25,250)) and plt.ylim(ymax = 250, ymin = 25). I am using the Agg backend.
  • Thanks to both of you. Does it work with PDF backend for you.
  • note: axisbg is now deprecated
  • plt.ylim is the correct, modern solution to this problem. Here is a good resource: showmecode.info/matplotlib/axes/set-limits

The Answer 1

705 people think this answer is useful

Try this . Works for subplots too .

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

The Answer 2

136 people think this answer is useful

Your code works also for me. However, another workaround can be to get the plot’s axis and then change only the y-values:

x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,25,250))

The Answer 3

103 people think this answer is useful

One thing you can do is to set your axis range by yourself by using matplotlib.pyplot.axis.

matplotlib.pyplot.axis

from matplotlib import pyplot as plt
plt.axis([0, 10, 0, 20])

0,10 is for x axis range. 0,20 is for y axis range.

or you can also use matplotlib.pyplot.xlim or matplotlib.pyplot.ylim

matplotlib.pyplot.ylim

plt.ylim(-2, 2)
plt.xlim(0,10)

The Answer 4

33 people think this answer is useful

You can instantiate an object from matplotlib.pyplot.axes and call the set_ylim() on it. It would be something like this:

import matplotlib.pyplot as plt
axes = plt.axes()
axes.set_ylim([0, 1])

The Answer 5

21 people think this answer is useful

This worked at least in matplotlib version 2.2.2:

plt.axis([None, None, 0, 100])

Probably this is a nice way to set up for example xmin and ymax only, etc.

The Answer 6

16 people think this answer is useful

To add to @Hima’s answer, if you want to modify a current x or y limit you could use the following.

import numpy as np # you probably alredy do this so no extra overhead
fig, axes = plt.subplot()
axes.plot(data[:,0], data[:,1])
xlim = axes.get_xlim()
# example of how to zoomout by a factor of 0.1
factor = 0.1 
new_xlim = (xlim[0] + xlim[1])/2 + np.array((-0.5, 0.5)) * (xlim[1] - xlim[0]) * (1 + factor) 
axes.set_xlim(new_xlim)

I find this particularly useful when I want to zoom out or zoom in just a little from the default plot settings.

The Answer 7

15 people think this answer is useful

Just for fine tuning. If you want to set only one of the boundaries of the axis and let the other boundary unchanged, you can choose one or more of the following statements

plt.xlim(right=xmax) #xmax is your value
plt.xlim(left=xmin) #xmin is your value
plt.ylim(top=ymax) #ymax is your value
plt.ylim(bottom=ymin) #ymin is your value

Take a look at the documentation for xlim and for ylim

The Answer 8

10 people think this answer is useful

This should work. Your code works for me, like for Tam├ís and Manoj Govindan. It looks like you could try to update Matplotlib. If you can’t update Matplotlib (for instance if you have insufficient administrative rights), maybe using a different backend with matplotlib.use() could help.

The Answer 9

0 people think this answer is useful

If an axes (generated by code below the code shown in the question) is sharing the range with the first axes, make sure that you set the range after the last plot of that axes.

Add a Comment