Based on the comment from mdurant I wrote some minimal example as a starting point for you:
import pylab as pl
pl.figure()
x = pl.rand(30)
x.sort()
y = pl.sin(2 * pl.pi * x) + 0.1 * pl.randn(30)
pl.plot(x, y)
def ondraw(event):
xlim = pl.gca().get_xlim()
y_sel = y[pl.logical_and(x > xlim[0], x < xlim[1])]
pl.gca().set_ylim((y_sel.min(), y_sel.max()))
pl.gcf().canvas.mpl_connect('draw_event', ondraw)
You might want to add more events to handle mouse movements etc.
Some comments:
- I'm using
pylab
, since it provides useful functions for generating examle data. But you can import the matplotlib
library as well.
- The
ondraw
event handler gets the current x limits of the current axes gca()
, selects y
values with corresponding x
coordinates within the x limits and sets new y limits determined by the minimum and maximum y value of selected coordinates.
- With
mpl_connect
we attach the event handler to the current axes. Every time the axes is drawn - e.g. due to new plotting commands or manual user interaction - it calls ondraw
to update the y limits.