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

Skip to content

Commit d443579

Browse files
committed
add Legend.set_draggable, Legend.get_draggable
1 parent bf2b799 commit d443579

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

lib/matplotlib/legend.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@
5252
class DraggableLegend(DraggableOffsetBox):
5353
def __init__(self, legend, use_blit=False, update="loc"):
5454
"""
55+
Wrapper around a `.Legend` to support mouse dragging.
56+
5557
Parameters
5658
----------
57-
update : string
58-
If "loc", update *loc* parameter of legend upon finalizing.
59-
If "bbox", update *bbox_to_anchor* parameter.
59+
legend : `.Legend`
60+
The `.Legend` instance to wrap.
61+
use_blit : bool, optional
62+
63+
update : ['loc' | 'bbox'], optional
64+
If "loc", update the *loc* parameter of the legend upon finalizing.
65+
If "bbox", update the *bbox_to_anchor* parameter.
6066
"""
6167
self.legend = legend
6268

@@ -1096,6 +1102,41 @@ def _find_best_position(self, width, height, renderer, consider=None):
10961102
def contains(self, event):
10971103
return self.legendPatch.contains(event)
10981104

1105+
def set_draggable(self, state, use_blit=False, update='loc'):
1106+
"""
1107+
Enable or disable mouse dragging support of the legend.
1108+
1109+
Parameters
1110+
----------
1111+
state : bool
1112+
``True`` / ``False`` enables / disables mouse dragging.
1113+
use_blit : bool, optional
1114+
1115+
update_loc : ['loc' | 'bbox'], optional
1116+
The legend parameter to be changed when dragged:
1117+
1118+
- 'loc': update the *loc* parameter of the legend
1119+
- 'bbox': update the *bbox_to_anchor* parameter of the legend
1120+
1121+
Returns
1122+
-------
1123+
If *state* is ``True`` this returns the `~.DraggableLegend` helper
1124+
instance. Otherwise this returns ``None``.
1125+
"""
1126+
if state:
1127+
if self._draggable is None:
1128+
self._draggable = DraggableLegend(self,
1129+
use_blit,
1130+
update=update)
1131+
else:
1132+
if self._draggable is not None:
1133+
self._draggable.disconnect()
1134+
self._draggable = None
1135+
1136+
def get_draggable(self):
1137+
"""Return ``True`` if the legend is draggable, ``False`` otherwise."""
1138+
return self._draggable is not None
1139+
10991140
def draggable(self, state=None, use_blit=False, update="loc"):
11001141
"""
11011142
Set the draggable state -- if state is
@@ -1114,21 +1155,10 @@ def draggable(self, state=None, use_blit=False, update="loc"):
11141155
when dragged. If update is "loc", the *loc* parameter of the legend
11151156
is changed. If "bbox", the *bbox_to_anchor* parameter is changed.
11161157
"""
1117-
is_draggable = self._draggable is not None
1118-
1119-
# if state is None we'll toggle
11201158
if state is None:
1121-
state = not is_draggable
1159+
state = not self.get_draggable() # toggle state
11221160

1123-
if state:
1124-
if self._draggable is None:
1125-
self._draggable = DraggableLegend(self,
1126-
use_blit,
1127-
update=update)
1128-
else:
1129-
if self._draggable is not None:
1130-
self._draggable.disconnect()
1131-
self._draggable = None
1161+
self.set_draggable(state, use_blit, update)
11321162

11331163
return self._draggable
11341164

0 commit comments

Comments
 (0)