Moving legend with mouse?

Oops, forgot to cc the list...

···

---------- Forwarded message ----------
From: Anthony Floyd <anthonyfloyd@...287...>
Date: Wed, Aug 6, 2008 at 12:28 PM
Subject: Re: [Matplotlib-users] Moving legend with mouse?
To: Søren Nielsen <soren.skou.nielsen@...287...>

On Fri, Aug 1, 2008 at 6:10 AM, Søren Nielsen <soren.skou.nielsen@...287...> wrote:

I'm using matplotlib 0.98 together with wxpython 2.8.8.0. It would be nice
to be able to click on the legend and move it with the mouse. Is that
possible? Is there a simple way to do this? Has anyone tried this or can
someone point in the direction?

Thanks,
Soren

Hi Soren,

Sorry for the delay. This is pretty easy to do if you're using the API.

Our code is pretty ... um, integrated, so I can't provide you with a
detailed step-by-step, but here's the general idea:

- On a FigureCanvas, use mpl_connect to hook into the
'motion_notify_event'. So, we have
"self.mpl_connect('motion_notify_event', self._onMouseMove)"
- onMouseMove checks to see if we've previously picked the legend and
adds mplEvent.x and mplEvent.y to the original pick location and calls
our self._moveLegend function
- The _moveLegend routine is:

   def _moveLegend(self, x, y, autoDraw=True):

       height = float(self.figure.bbox.height())
       width = float(self.figure.bbox.width())

       dx = x/width
       dy = y/height

       self._legendLocation = (dx, dy)
       self.plot.getLegend().setLocation(self._legendLocation)

       self._legend._loc=(dx,dy)

       if autoDraw:
           self.draw()

Now, you won't need most of this, but the important things here are
the dx,dy calculations and the _legend._loc=(dx,dy) assignment.

It's not a very complete set of instructions, but it might get you
pointed in the right direction. Legend dragging in this manner works
quite smoothly for us.

Regards,
Anthony.