Thank you for trying to help me, though I can't see how aspect is going
to help me. As I understand of the documentation, it would require me to know the
relationship between x and y, which I don't.
I can calculate it, but since it varies between each change in xlimits
and different plot, it would be the same as calculating the ylimits.
As for pyplot.xlim(xmin,xmax) (suggested by Ethan Swint), it does the same as ax.set_xlim() for me.
Below is a sample code that will illustrate the problem.
####### Start code ############
matplotlib.use('Qt4Agg') # generate postscript output by default
import matplotlib.pyplot as plt
import numpy as np
# Creating a function to plot
x = np.linspace(-10, 10, 200)
p = np.poly1d([2, 3, 1, 4])
y = p(x) * np.sin(x)
# plotting the full figure
fig = plt.figure()
ax = fig.add_subplot(111)
plt.title('Full graph. (Press key for next plot)')
# This is how I'm currently doing it
# x limits
xmin = 2
xmax = 6
# Calculating y limits
ymin = y[np.logical_and(xmin < x, x < xmax)].min()
ymax = y[np.logical_and(xmin < x, x < xmax)].max()
# Inserting some room
room = np.maximum(np.abs(ymin * 0.05), np.abs(ymax * 0.05))
ymin = ymin + room * np.sign(ymin)
ymax = ymax + room * np.sign(ymax)
# Setting the limits
plt.title('What I want (Press key for next plot)')
# This is what pyplot does by default if I only set the limits
plt.title('What I get if I only use set_xlim (Press key for exit)')
####### End code ############
Actually, the autoscaling is working as it should in your case. By default, automatic limits will encompass all data plotted.
On Friday, February 3, 2012, Pål Gunnar Ellingsen <firstname.lastname@example.org> wrote:
> I've got a plot, containing a graph.
> I would like to look at certain parts of it.
> To do this I zoom in on the x-axis using set_xlim()
> Then I would like to call autoscale(axis='y') and have matplotlib
> autoscale the y axis within the current x-axis.
> I've tried several ways of doing this, both with autoscale and set_ylim(auto=True)
> but non of them have been able to do this, as they only autoscale y on the full graph.
> In the end I ended up writing my own code for calculating the new ymin and ymax ans using set_ylim([ymin, ymax]),
> but I feel that this should not be necessary.
> While googling for the a solution I found an earlier e-mail on the matplotlib list,
> which describes the same problem, but was never answered.
> Is there any other solution for this that I've missed?
The missing piece of your puzzle is specifying an aspect constraint. ax.set_aspect() will allow for some sort of relationship between the x and y axes to exist. For example, setting it to 'equal' means that limits are chosen such that a distance on the x axis is displayed the same way as the same distance in the y-axis. There are some other preset values and I think you can specify a float as well.
This is what I can remember of the top of my head. I hope this helps!