Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add draggable as param to Legend init #23913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
The custom dictionary mapping instances or types to a legend
handler. This *handler_map* updates the default handler map
found at `matplotlib.legend.Legend.get_legend_handler_map`.

draggable : bool, default: False
Whether the legend can be dragged with the mouse.
""")


Expand Down Expand Up @@ -342,7 +345,8 @@ def __init__(
title_fontproperties=None, # properties for the legend title
alignment="center", # control the alignment within the legend box
*,
ncol=1 # synonym for ncols (backward compatibility)
ncol=1, # synonym for ncols (backward compatibility)
draggable=False # whether the legend can be dragged with the mouse
):
"""
Parameters
Expand Down Expand Up @@ -537,7 +541,9 @@ def val_or_rc(val, rc_name):
title_prop_fp.set_size(title_fontsize)

self.set_title(title, prop=title_prop_fp)

self._draggable = None
self.set_draggable(state=draggable)

# set the text color

Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ def test_get_set_draggable():
assert not legend.get_draggable()


@pytest.mark.parametrize('draggable', (True, False))
def test_legend_draggable(draggable):
fig, ax = plt.subplots()
ax.plot(range(10), label='shabnams')
leg = ax.legend(draggable=draggable)
assert leg.get_draggable() is draggable


def test_alpha_handles():
x, n, hh = plt.hist([1, 2, 3], alpha=0.25, label='data', color='red')
legend = plt.legend()
Expand Down