Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Bug]: Draggable Legend is out of control unless it has a picker set. #30303

Closed
@brobr

Description

@brobr

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 thecursor 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 theLegend, being anArtist, 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 fromNone, which kills draggability - results in expected draggability behavior.
Maybe a callself.set_picker(1) or so during initialization of a draggable legend could solve this issue?

Code for reproduction

#!/usr/bin/env python3#importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.textimportTextt=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 artefactlines= [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.forlegend_line,ax_lineinzip(leg.get_lines(),lines):legend_line.set_picker(pickradius)# Enable picking on the legend line.map_legend_to_ax[legend_line]=ax_linedefon_pick(event):# On the pick event, find the original line corresponding to the legend# proxy line, and toggle its visibility.legend_line=event.artistifisinstance(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.iflegend_linenotinmap_legend_to_ax:returnax_line=map_legend_to_ax[legend_line]visible=notax_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.0ifvisibleelse0.2)fig.canvas.draw()#leg.set_picker(None) # no effect on set_draggable artefactfig.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-behaviourprint (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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp