Skip to content

Commit b2d38a2

Browse files
committed
Cleanup and add test
1 parent e4ee185 commit b2d38a2

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

examples/text_labels_and_annotations/legend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
ax.plot(a, d, 'k:', label='Data length')
2222
ax.plot(a, c + d, 'k', label='Total message length')
2323

24-
#Create an arrow with pre-defined label.
24+
# Create an arrow with pre-defined label.
2525
ax.annotate("", xy=(1.5, 4.5), xytext=(1.5, 9.0),
26-
arrowprops={'arrowstyle':'<->', 'color':'C7'}, label='Distance')
26+
arrowprops={'arrowstyle': '<->', 'color': 'C7'}, label='Distance')
2727

2828
legend = ax.legend(loc='upper center', shadow=True, fontsize='large')
2929

lib/matplotlib/legend_handler.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ class HandlerFancyArrowPatch(HandlerPatch):
812812
"""
813813
Handler for `~.FancyArrowPatch` instances.
814814
"""
815-
def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize):
815+
def _create_patch(self, legend, orig_handle, xdescent, ydescent, width,
816+
height, fontsize):
816817
arrow = FancyArrowPatch([-xdescent,
817818
-ydescent + height / 2],
818819
[-xdescent + width,
@@ -826,32 +827,12 @@ class HandlerAnnotation(HandlerBase):
826827
"""
827828
Handler for `.Annotation` instances.
828829
Defers to `HandlerFancyArrowPatch` to draw the annotation arrow (if any).
829-
830-
Parameters
831-
----------
832-
pad : float, optional
833-
If None, fall back to :rc:`legend.borderpad` as the default.
834-
In units of fraction of font size. Default is None
835-
.
836-
width_ratios : two-tuple, optional
837-
The relative width of the respective text/arrow legend annotation pair.
838-
Default is (1, 4).
839830
"""
840-
841-
def __init__(self, pad=None, width_ratios=(1, 4), **kwargs):
842-
843-
self._pad = pad
844-
self._width_ratios = width_ratios
845-
846-
HandlerBase.__init__(self, **kwargs)
847-
848831
def create_artists(self, legend, orig_handle, xdescent, ydescent, width,
849832
height, fontsize, trans):
850833

851834
if orig_handle.arrow_patch is not None:
852-
853835
# Arrow without text
854-
855836
handler = HandlerFancyArrowPatch()
856837
handle = orig_handle.arrow_patch
857838

lib/matplotlib/tests/test_legend.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,22 @@ def test_legend_markers_from_line2d():
927927

928928
assert markers == new_markers == _markers
929929
assert labels == new_labels
930+
931+
932+
def test_annotation_legend():
933+
fig, ax = plt.subplots()
934+
ax.annotate("", xy=(0.5, 0.5), xytext=(0.5, 0.7),
935+
arrowprops={'arrowstyle': '<->'}, label="Bar")
936+
legend = ax.legend()
937+
assert len(legend.get_texts()) == 1
938+
ax.annotate("Foo", xy=(0.3, 0.3))
939+
legend = ax.legend()
940+
assert len(legend.get_texts()) == 1
941+
ax.annotate("FooBar", xy=(0.7, 0.7), xytext=(0.7, 0.9),
942+
arrowprops={'arrowstyle': '->'})
943+
legend = ax.legend()
944+
assert len(legend.get_texts()) == 1
945+
ax.annotate("Foo", xy=(0.1, 0.1), xytext=(0.1, 0.7),
946+
arrowprops={'arrowstyle': '<-'}, label="Foo")
947+
legend = ax.legend()
948+
assert len(legend.get_texts()) == 2

0 commit comments

Comments
 (0)