On Tue, Mar 24, 2009 at 11:37 PM, C M <cmpyt...@gmail.com> wrote: > Using mpl 0.98.5.2 in OO mode with wxAgg backend. > > I'm trying to make my legends draggable. It works, but > there is a some inaccuracy with positioning. As I drag it, > the cursor "outruns" the position of the legend, and that > error grows the further away from the initial starting point > the cursor has been moved. It makes the feel of dragging > completely wrong. > > The work of repositioning the legend is done in an > on_motion event. self.figure here is a mpl Figure. > The motion event handler is: > > #drag the legend > def on_motion(self, event): > height = float(self.figure.bbox.height) > width = float(self.figure.bbox.width) > > dx = event.x/width > dy = event.y/height > > if self.gotLegend == 1: #it's picked up > self.legend._loc=(dx,dy) #reposition it > self.canvas.draw() > self.parent.Refresh() >
Weird. This is essentially what we do, but in our case there's no problem. I ran your example and experienced the same "outrun" behavior so I checked to see what was different between yours and ours. In ours, we catch the mpl button down event and after establishing a hit on the legend do: bbox = self._legend.get_window_extent() self._dragOffset = (bbox.xmin() - mplEvent.x, bbox.ymin() - mplEvent.y) Then in the mousemove event handler we do: self._moveLegend(mplEvent.x + self._dragOffset[0], mplEvent.y + self._dragOffset[1]) where self._moveLegend 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 # The following two lines are specific to our implementation and our internal data classes self._legendLocation = (dx, dy) self.plot.getLegend().setLocation(self._legendLocation) # This is the line that "moves" the legend self._legend._loc=(dx,dy) if autoDraw: self.draw() Now we're using an ancient version of MPL (0.90.1) and many things have changed in the meantime. YMMV. A> ------------------------------------------------------------------------------ _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users