autoscale y for current x range on display

Hi, I am new to python and matplotlib and have a small question.

Is there a way to autoscale the y axis for the current x range in view?

Currently, my y axes are scaled to all the data.

Thanks

Adrian

Adrian HILL, on 2011-03-23 19:42, wrote:

Hi, I am new to python and matplotlib and have a small question.

Is there a way to autoscale the y axis for the current x range in view?

Currently, my y axes are scaled to all the data.

Hi Adrian,

there isn't a built-in way of doing this at the moment, because
the general case would require going through all of the plotted
data and finding the min&max for only the parts of the data that
are withing some range (as apposed to the min&max for all data).

If there's some regularity about your data, or if you know that
you'll have only a few (maybe even just one?) artist, you could
implement such functionality using callbacks, such as
'xlim_changed' or 'ylim_changed'.

The quickest way would be to have those callbacks adjust
ax.dataLim and then call autoscale.

plt.clf()
ax = plt.gca()
data = np.sin(np.linspace(0,10,100))
ax.plot(data)

def cb(ax):
   start,stop = [int(x) for x in ax.get_xlim()]
   d = data[start:stop+1]
   ax.dataLim._points[:,1] = d.min(), d.max()
   # the previous line would need to change depending on your
   # data
   ax.autoscale_view(scalex=False,scaley=True)
   plt.draw()

ax.callbacks.connect('xlim_changed',cb)

plt.xlim(0,30)
# for the next line to work in ipython you'll need to
# paste/or cpaste this entire script, or just pause before
# changing the xlim again to verify that it works as intended
raw_input("press return for to rescale")
plt.xlim(0,55)

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7