31
31
from matplotlib import rcParams
32
32
from matplotlib import docstring
33
33
from matplotlib .artist import Artist , allow_rasterization
34
- from matplotlib .cbook import silent_list , is_hashable
34
+ from matplotlib .cbook import silent_list , is_hashable , warn_deprecated
35
35
import matplotlib .colors as colors
36
36
from matplotlib .font_manager import FontProperties
37
37
from matplotlib .lines import Line2D
52
52
class DraggableLegend (DraggableOffsetBox ):
53
53
def __init__ (self , legend , use_blit = False , update = "loc" ):
54
54
"""
55
+ Wrapper around a `.Legend` to support mouse dragging.
56
+
55
57
Parameters
56
58
----------
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
+ Use blitting for faster image composition. For details see
63
+ :ref:`func-animation`.
64
+ update : {'loc', 'bbox'}, optional
65
+ If "loc", update the *loc* parameter of the legend upon finalizing.
66
+ If "bbox", update the *bbox_to_anchor* parameter.
60
67
"""
61
68
self .legend = legend
62
69
@@ -1110,6 +1117,43 @@ def _find_best_position(self, width, height, renderer, consider=None):
1110
1117
def contains (self , event ):
1111
1118
return self .legendPatch .contains (event )
1112
1119
1120
+ def set_draggable (self , state , use_blit = False , update = 'loc' ):
1121
+ """
1122
+ Enable or disable mouse dragging support of the legend.
1123
+
1124
+ Parameters
1125
+ ----------
1126
+ state : bool
1127
+ ``True`` / ``False`` enables / disables mouse dragging.
1128
+ use_blit : bool, optional
1129
+ Use blitting for faster image composition. For details see
1130
+ :ref:`func-animation`.
1131
+ update : ['loc' | 'bbox'], optional
1132
+ The legend parameter to be changed when dragged:
1133
+
1134
+ - 'loc': update the *loc* parameter of the legend
1135
+ - 'bbox': update the *bbox_to_anchor* parameter of the legend
1136
+
1137
+ Returns
1138
+ -------
1139
+ If *state* is ``True`` this returns the `~.DraggableLegend` helper
1140
+ instance. Otherwise this returns ``None``.
1141
+ """
1142
+ if state :
1143
+ if self ._draggable is None :
1144
+ self ._draggable = DraggableLegend (self ,
1145
+ use_blit ,
1146
+ update = update )
1147
+ else :
1148
+ if self ._draggable is not None :
1149
+ self ._draggable .disconnect ()
1150
+ self ._draggable = None
1151
+ return self ._draggable
1152
+
1153
+ def get_draggable (self ):
1154
+ """Return ``True`` if the legend is draggable, ``False`` otherwise."""
1155
+ return self ._draggable is not None
1156
+
1113
1157
def draggable (self , state = None , use_blit = False , update = "loc" ):
1114
1158
"""
1115
1159
Set the draggable state -- if state is
@@ -1128,21 +1172,16 @@ def draggable(self, state=None, use_blit=False, update="loc"):
1128
1172
when dragged. If update is "loc", the *loc* parameter of the legend
1129
1173
is changed. If "bbox", the *bbox_to_anchor* parameter is changed.
1130
1174
"""
1131
- is_draggable = self ._draggable is not None
1175
+ warn_deprecated ("2.2" ,
1176
+ message = "Legend.draggable() is drepecated in "
1177
+ "favor of Legend.set_draggable(). "
1178
+ "Legend.draggable may be reintroduced as a "
1179
+ "property in future releases." )
1132
1180
1133
- # if state is None we'll toggle
1134
1181
if state is None :
1135
- state = not is_draggable
1182
+ state = not self . get_draggable () # toggle state
1136
1183
1137
- if state :
1138
- if self ._draggable is None :
1139
- self ._draggable = DraggableLegend (self ,
1140
- use_blit ,
1141
- update = update )
1142
- else :
1143
- if self ._draggable is not None :
1144
- self ._draggable .disconnect ()
1145
- self ._draggable = None
1184
+ self .set_draggable (state , use_blit , update )
1146
1185
1147
1186
return self ._draggable
1148
1187
0 commit comments