Zooming with the mouse

Hello all!

I just recently downloaded Matplotlib and love it so far :slight_smile:

A hopefully quick question. In all the demo examples, the navigation bar
has buttons for incremental +/- zoom in the X/Y directions, but no way
to directly choose a rectange within the figure window to zoom to like
in matlab. It gets very tedious the way it is if a lot of zooming and
panning is involved.

I found that I could draw a rectange in the axes and update its
coordinates on an motion_notify_event. The problem is I do not know how
to link the event.get_pointer() coordinates to the coordinates within
the axes. Therefore I cannot set the coordinates of the rectangle
correctly. The following code which is slightly modified from
embed_gtk2.py demonstrates what I am trying to do...




from matplotlib.numerix import arange, sin, cos, pi

import matplotlib
from matplotlib.backends.backend_gtk import FigureCanvasGTK

from matplotlib.axes import Subplot
from matplotlib.figure import Figure
import gtk

class MyApp:
    def __init__(self):
        self.win = gtk.Window()
        self.win.set_name("Embedding in GTK")
        self.win.connect("destroy", gtk.mainquit)

        self.vbox = gtk.VBox(spacing=3)

        self.fig = Figure(figsize=(5,4), dpi=100)
        self.ax = Subplot(self.fig, 111)

        self.recth = 0.2
        self.ylast = 0

        x = [0,0.5,0.5,0,0]
        y = [0,0,self.recth,self.recth,0]
        t = [0,1]
        r = [0,1]

        self.h = self.ax.plot(x,y,t,r)

        self.canvas = FigureCanvasGTK(self.fig) # a gtk.DrawingArea
        self.canvas.connect('motion_notify_event', self.findClick)


    def findClick(self, widget, event):
        ynow = event.get_coords()[1]
        if ynow < self.ylast:
            self.recth += 0.01
        elif ynow > self.ylast:
            self.recth -= 0.01

        self.ylast = ynow

if __name__=='__main__':
    app = MyApp()