Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 88b3783

Browse filesBrowse files
committed
Cleanup and add test
1 parent 647930b commit 88b3783
Copy full SHA for 88b3783

File tree

Expand file treeCollapse file tree

3 files changed

+36
-23
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+36
-23
lines changed

‎examples/text_labels_and_annotations/legend.py

Copy file name to clipboardExpand all lines: examples/text_labels_and_annotations/legend.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/legend_handler.py
+7-21Lines changed: 7 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,34 +827,19 @@ 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
838+
else:
839+
# No arrow
840+
handler = HandlerPatch()
841+
# Dummy patch to copy properties from to rectangle patch
842+
handle = Rectangle(width=0, height=0, xy=(0, 0), color='none')
857843

858844
return handler.create_artists(legend, handle, xdescent, ydescent,
859845
width, height, fontsize, trans)

‎lib/matplotlib/tests/test_legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_legend.py
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,30 @@ 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+
# Add annotation with arrow and label
935+
ax.annotate("", xy=(0.5, 0.5), xytext=(0.5, 0.7),
936+
arrowprops={'arrowstyle': '<->'}, label="Bar")
937+
legend = ax.legend()
938+
assert len(legend.get_texts()) == 1
939+
# No arrow, no label
940+
ax.annotate("Foo", xy=(0.3, 0.3))
941+
legend = ax.legend()
942+
assert len(legend.get_texts()) == 1
943+
# Arrow, no label
944+
ax.annotate("FooBar", xy=(0.7, 0.7), xytext=(0.7, 0.9),
945+
arrowprops={'arrowstyle': '->'})
946+
legend = ax.legend()
947+
assert len(legend.get_texts()) == 1
948+
# Add another annotation with arrow and label. now with non-empty text
949+
ax.annotate("Foo", xy=(0.1, 0.1), xytext=(0.1, 0.7),
950+
arrowprops={'arrowstyle': '<-'}, label="Foo")
951+
legend = ax.legend()
952+
assert len(legend.get_texts()) == 2
953+
# Add annotation without arrow, but with label
954+
ax.annotate("Foo", xy=(0.2, 0.2), xytext=(0.2, 0.6), label="Foo")
955+
legend = ax.legend()
956+
assert len(legend.get_texts()) == 3

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.