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

Skip to content

Commit d2968e6

Browse files
authored
Merge pull request #10050 from timhoffm/legend-draggable
Update Legend draggable API
2 parents c098519 + 6a82c9e commit d2968e6

File tree

5 files changed

+92
-17
lines changed

5 files changed

+92
-17
lines changed

doc/api/animation_api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ To save an animation to disk use `Animation.save` or `Animation.to_html5_video`
3636
See :ref:`ani_writer_classes` below for details about what movie formats are
3737
supported.
3838

39+
40+
.. _func-animation:
41+
3942
``FuncAnimation``
4043
-----------------
4144

doc/api/api_changes/2017-12-20-TH.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Deprecations
2+
````````````
3+
4+
`.Legend.draggable()` is drepecated in favor of `.Legend.set_draggable()`.
5+
``Legend.draggable`` may be reintroduced as a property in future releases.

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def apply_callback(data):
242242
ncol = old_legend._ncol
243243
new_legend = axes.legend(ncol=ncol)
244244
if new_legend:
245-
new_legend.draggable(draggable)
245+
new_legend.set_draggable(draggable)
246246

247247
# Redraw
248248
figure = axes.get_figure()

lib/matplotlib/legend.py

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from matplotlib import rcParams
3232
from matplotlib import docstring
3333
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
3535
import matplotlib.colors as colors
3636
from matplotlib.font_manager import FontProperties
3737
from matplotlib.lines import Line2D
@@ -52,11 +52,18 @@
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+
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

lib/matplotlib/tests/test_legend.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import matplotlib.collections as mcollections
1313
from matplotlib.legend_handler import HandlerTuple
1414
import matplotlib.legend as mlegend
15+
from matplotlib.cbook.deprecation import MatplotlibDeprecationWarning
1516

1617

1718
def test_legend_ordereddict():
@@ -511,3 +512,30 @@ def test_legend_title_fontsize():
511512
ax.plot(range(10))
512513
leg = ax.legend(title='Aardvark', title_fontsize=22)
513514
assert leg.get_title().get_fontsize() == 22
515+
516+
517+
def test_get_set_draggable():
518+
legend = plt.legend()
519+
assert not legend.get_draggable()
520+
legend.set_draggable(True)
521+
assert legend.get_draggable()
522+
legend.set_draggable(False)
523+
assert not legend.get_draggable()
524+
525+
526+
def test_draggable():
527+
legend = plt.legend()
528+
with pytest.warns(MatplotlibDeprecationWarning):
529+
legend.draggable(True)
530+
assert legend.get_draggable()
531+
with pytest.warns(MatplotlibDeprecationWarning):
532+
legend.draggable(False)
533+
assert not legend.get_draggable()
534+
535+
# test toggle
536+
with pytest.warns(MatplotlibDeprecationWarning):
537+
legend.draggable()
538+
assert legend.get_draggable()
539+
with pytest.warns(MatplotlibDeprecationWarning):
540+
legend.draggable()
541+
assert not legend.get_draggable()

0 commit comments

Comments
 (0)