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.
If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number.
set_aspect() to Make a Square Plot With Equal Axes Axes. set_aspect() function. If we use "equal" as an aspect ratio in the function, we get a plot with the same scaling from data points to plot units for X-axis and Y-axis. It sets both X-axis and Y-axis to have the same range.
Set y-Axis Limits for Specific AxesCall the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Then set the y-axis limits for the bottom plot by specifying ax2 as the first input argument to ylim .
In what sense do you mean inspecting the data is cumbersome? If in terms of writing code, then it's not so bad. Try something like
pylab.ylim(numpy.min(data), numpy.max(data))
...where data
can be numpy.arange(100)[0:11]
.
In the general case, if you have xdata
and ydata
(but supposing they are sorted) you would have to so something like
from bisect import bisect
sub_ydata = ydata[bisect(xdata, xmin):bisect(xdata, xmax)]
pylab.ylim(numpy.min(sub_ydata), numpy.max(sub_ydata))
If you mean that it's a hard thing computationally, then I don't really see how matplotlib
could perform it without such calculations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With