Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Add legend handler for Annotation#23160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
oscargus wants to merge2 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromoscargus:legendannotate
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Cleanup and add test
  • Loading branch information
@oscargus
oscargus committedMay 29, 2022
commit88b37839b958a8b7c0fb1c85b6e561a12cae7e6c
4 changes: 2 additions & 2 deletionsexamples/text_labels_and_annotations/legend.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,9 +21,9 @@
ax.plot(a, d, 'k:', label='Data length')
ax.plot(a, c + d, 'k', label='Total message length')

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

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

Expand Down
28 changes: 7 additions & 21 deletionslib/matplotlib/legend_handler.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,7 +812,8 @@ class HandlerFancyArrowPatch(HandlerPatch):
"""
Handler for `~.FancyArrowPatch` instances.
"""
def _create_patch(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize):
def _create_patch(self, legend, orig_handle, xdescent, ydescent, width,
height, fontsize):
arrow = FancyArrowPatch([-xdescent,
-ydescent + height / 2],
[-xdescent + width,
Expand All@@ -826,34 +827,19 @@ class HandlerAnnotation(HandlerBase):
"""
Handler for `.Annotation` instances.
Defers to `HandlerFancyArrowPatch` to draw the annotation arrow (if any).

Parameters
----------
pad : float, optional
If None, fall back to :rc:`legend.borderpad` as the default.
In units of fraction of font size. Default is None
.
width_ratios : two-tuple, optional
The relative width of the respective text/arrow legend annotation pair.
Default is (1, 4).
"""

def __init__(self, pad=None, width_ratios=(1, 4), **kwargs):

self._pad = pad
self._width_ratios = width_ratios

HandlerBase.__init__(self, **kwargs)

def create_artists(self, legend, orig_handle, xdescent, ydescent, width,
height, fontsize, trans):

if orig_handle.arrow_patch is not None:

# Arrow without text

handler = HandlerFancyArrowPatch()
handle = orig_handle.arrow_patch
else:
# No arrow
handler = HandlerPatch()
# Dummy patch to copy properties from to rectangle patch
handle = Rectangle(width=0, height=0, xy=(0, 0), color='none')

return handler.create_artists(legend, handle, xdescent, ydescent,
width, height, fontsize, trans)
27 changes: 27 additions & 0 deletionslib/matplotlib/tests/test_legend.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -927,3 +927,30 @@ def test_legend_markers_from_line2d():

assert markers == new_markers == _markers
assert labels == new_labels


def test_annotation_legend():
fig, ax = plt.subplots()
# Add annotation with arrow and label
ax.annotate("", xy=(0.5, 0.5), xytext=(0.5, 0.7),
arrowprops={'arrowstyle': '<->'}, label="Bar")
legend = ax.legend()
assert len(legend.get_texts()) == 1
# No arrow, no label
ax.annotate("Foo", xy=(0.3, 0.3))
legend = ax.legend()
assert len(legend.get_texts()) == 1
# Arrow, no label
ax.annotate("FooBar", xy=(0.7, 0.7), xytext=(0.7, 0.9),
arrowprops={'arrowstyle': '->'})
legend = ax.legend()
assert len(legend.get_texts()) == 1
# Add another annotation with arrow and label. now with non-empty text
ax.annotate("Foo", xy=(0.1, 0.1), xytext=(0.1, 0.7),
arrowprops={'arrowstyle': '<-'}, label="Foo")
legend = ax.legend()
assert len(legend.get_texts()) == 2
# Add annotation without arrow, but with label
ax.annotate("Foo", xy=(0.2, 0.2), xytext=(0.2, 0.6), label="Foo")
legend = ax.legend()
assert len(legend.get_texts()) == 3

[8]ページ先頭

©2009-2025 Movatter.jp