3131from matplotlib import rcParams
3232from matplotlib import docstring
3333from 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
3535import matplotlib .colors as colors
3636from matplotlib .font_manager import FontProperties
3737from matplotlib .lines import Line2D
5252class 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+ 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.
6067 """
6168 self .legend = legend
6269
@@ -1110,6 +1117,43 @@ def _find_best_position(self, width, height, renderer, consider=None):
11101117 def contains (self , event ):
11111118 return self .legendPatch .contains (event )
11121119
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+
11131157 def draggable (self , state = None , use_blit = False , update = "loc" ):
11141158 """
11151159 Set the draggable state -- if state is
@@ -1128,21 +1172,16 @@ def draggable(self, state=None, use_blit=False, update="loc"):
11281172 when dragged. If update is "loc", the *loc* parameter of the legend
11291173 is changed. If "bbox", the *bbox_to_anchor* parameter is changed.
11301174 """
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." )
11321180
1133- # if state is None we'll toggle
11341181 if state is None :
1135- state = not is_draggable
1182+ state = not self . get_draggable () # toggle state
11361183
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 )
11461185
11471186 return self ._draggable
11481187
0 commit comments