I'm trying to plot a subset of some data, but the y-axis limits are not updated properly after I set the x-axis limits. Is there a way to have matplotlib update the y-axis limits after setting the x-axis limits?
For example, consider the following plot:
import numpy
import pylab
pylab.plot(numpy.arange(100)**2.0)
which gives: 
which works fine. However if I want to only view the part from x=0 to x=10, the y-scaling is messed up:
pylab.plot(numpy.arange(100)**2.0)
pylab.xlim(0,10)
which gives:
.
In the former case, the x- and y-axis are scaled properly, in the latter case, the y-axis is still scaled the same, even if the data is not plotted. How do I tell matplotlib to update the y-axis scaling?
Obvious workarounds would be to plot a subset of the data itself, or to reset the y-axis limits manually by inspecting the data, but those are both rather cumbersome.
Update:
The example above is simplified, in the more general case one has:
pylab.plot(xdata, ydata1)
pylab.plot(xdata, ydata2)
pylab.plot(xdata, ydata3)
pylab.xlim(xmin, xmax)
Setting the y-axis range manually is of course possible
subidx = N.argwhere((xdata >= xmin) & (xdata <= xmax))
ymin = N.min(ydata1[subidx], ydata2[subidx], ydata3[subidx])
ymax = N.max(ydata1[subidx], ydata2[subidx], ydata3[subidx])
pylab.xlim(xmin, xmax)
but this is cumbersome to say the least (imho). Is there a faster way to do this without manually calculating the plotranges? Thanks!
Update 2:
The function autoscale does some scaling and seems the right candidate for this job, but treats the axes independently and only scales to the full data range, no matter what the axes limits are.