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

Skip to content

Commit df69d4f

Browse files
authored
Merge pull request #23913 from shabnamsadegh/legend_draggable_as_param
Add draggable as param to Legend init
2 parents 5c45952 + b69cbe5 commit df69d4f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/matplotlib/legend.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
286286
The custom dictionary mapping instances or types to a legend
287287
handler. This *handler_map* updates the default handler map
288288
found at `matplotlib.legend.Legend.get_legend_handler_map`.
289+
290+
draggable : bool, default: False
291+
Whether the legend can be dragged with the mouse.
289292
""")
290293

291294

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

539543
self.set_title(title, prop=title_prop_fp)
544+
540545
self._draggable = None
546+
self.set_draggable(state=draggable)
541547

542548
# set the text color
543549

lib/matplotlib/tests/test_legend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,14 @@ def test_get_set_draggable():
783783
assert not legend.get_draggable()
784784

785785

786+
@pytest.mark.parametrize('draggable', (True, False))
787+
def test_legend_draggable(draggable):
788+
fig, ax = plt.subplots()
789+
ax.plot(range(10), label='shabnams')
790+
leg = ax.legend(draggable=draggable)
791+
assert leg.get_draggable() is draggable
792+
793+
786794
def test_alpha_handles():
787795
x, n, hh = plt.hist([1, 2, 3], alpha=0.25, label='data', color='red')
788796
legend = plt.legend()

0 commit comments

Comments
 (0)