Description
Bug summary
The draggable legend as described in the Documentation (https://matplotlib.org/stable/gallery/event_handling/legend_picking.html) is behaving in an unexpected manner.
Click and drag the mouse when the cursor is far away from the legend moves the legend.
From a usability point of view it would make sense that only when the cursor is over the legend that the legend can be moved by dragging the mouse.
Solution (?). It appears that the Legend
, being an Artist
, has a picker_function that is not returning a value: print(legend.get_picker()
gives for example <function DraggableBase._picker at 0x7f95a0151bc0>
.
By setting a picker with any value - apart from None
, which kills draggability - results in expected draggability behavior.
Maybe a call self.set_picker(1)
or so during initialization of a draggable legend could solve this issue?
Code for reproduction
#!/usr/bin/env python3
#
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.text import Text
t = np.linspace(0, 1)
y1 = 2 * np.sin(2 * np.pi * t)
y2 = 4 * np.sin(2 * np.pi * 2 * t)
fig, ax = plt.subplots()
title = ax.set_title('Click this title once to control draggable legend')
title.set_picker(5)
(line1, ) = ax.plot(t, y1, lw=2, label='1 Hz')
(line2, ) = ax.plot(t, y2, lw=2, label='2 Hz')
leg = ax.legend(fancybox=True, shadow=True)
#leg.set_picker(None) # no effect on set_draggable artefact
lines = [line1, line2]
map_legend_to_ax = {} # Will map legend lines to original lines.
pickradius = 5 # Points (Pt). How close the click needs to be to trigger an event.
for legend_line, ax_line in zip(leg.get_lines(), lines):
legend_line.set_picker(pickradius) # Enable picking on the legend line.
map_legend_to_ax[legend_line] = ax_line
def on_pick(event):
# On the pick event, find the original line corresponding to the legend
# proxy line, and toggle its visibility.
legend_line = event.artist
if isinstance(event.artist, Text):
leg.set_picker(0)
print(f"Picker set on legend: {leg.get_picker()}\n"
"Now, only mouse_press on legend makes it move.")
# Do nothing if the source of the event is not a legend line.
if legend_line not in map_legend_to_ax:
return
ax_line = map_legend_to_ax[legend_line]
visible = not ax_line.get_visible()
ax_line.set_visible(visible)
# Change the alpha on the line in the legend, so we can see what lines
# have been toggled.
legend_line.set_alpha(1.0 if visible else 0.2)
fig.canvas.draw()
#leg.set_picker(None) # no effect on set_draggable artefact
fig.canvas.mpl_connect('pick_event', on_pick)
#leg.set_picker(None) # no effect on set_draggable artefact
# Works even if the legend is draggable. This is independent from picking legend lines.
leg.set_draggable(True)
#leg.set_picker(None) # kills set_draggable
#leg.set_draggable(True) # cannot restore set_draggable
#leg.set_picker(0) # creates expected draggable-legend-behaviour
print (f"Legend picker: {leg.get_picker()}; \n"
"legend can be moved by mouse_drag far away from legend.")
plt.show()
Actual outcome
Drag the mouse-pointer (press button + move) anywhere over the canvas outwith the legend and the legend will move as well.
Expected outcome
Drag the mouse over the canvas outwith the legend and the legend will NOT move. The legend will only move when dragged by the mouse directly.
Additional information
No response
Operating system
Slackware linux, 64-bit, current
Matplotlib Version
3.10.3
Matplotlib Backend
TkAgg
Python version
3.12.11
Jupyter version
No response
Installation
from source (.tar.gz)