Skip to content

Commit 5e1b5fe

Browse files
committed
Added test
1 parent e4ee185 commit 5e1b5fe

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
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 & 3 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,
@@ -849,9 +850,7 @@ def create_artists(self, legend, orig_handle, xdescent, ydescent, width,
849850
height, fontsize, trans):
850851

851852
if orig_handle.arrow_patch is not None:
852-
853853
# Arrow without text
854-
855854
handler = HandlerFancyArrowPatch()
856855
handle = orig_handle.arrow_patch
857856

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)