views:

50

answers:

2

Hi,

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.

Thanks in advance for any help.

A: 

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.

EOL
Thanks for checking! I am using the pdf backend (`matplotlib.use('PDF')`). I am using the version that comes with latest version of the Enthought Python Distribution. Can you please see if it works with the PDF backend. Thanks!
Curious2learn
It does work with the PDF backend, on Mac OS X. Are you sure that the output file is indeed updated with `plt.savefig()`?
EOL
I realized the problem, I think. If I take out `aPlot =` in the `plt.subplot` line it works for me too. It seems that if one assigns the subplot to a variable like that, some other method of setting the axes limits must be used. Is that true?
Curious2learn
As far as I know, `plt.ylim()` applies the limits to the current axes, which are set when you do `plt.subplot()`. I also can't believe that `plt.subplot()` care about how the axes it returns are used (put into a variable or not, etc.). So I'd say it should work; it does work on my machine.
EOL
A: 

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

thetarro