10000 Create explict rename legend entry section in guide · matplotlib/matplotlib@9e63be3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e63be3

Browse files
story645rcomer
andcommitted
Create explict rename legend entry section in guide
Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
1 parent 3ad0bc5 commit 9e63be3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

galleries/users_explain/axes/legend_guide.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
line_down, = ax.plot([3, 2, 1], label='Line 1')
6060
ax.legend(handles=[line_up, line_down])
6161
62+
Renaming legend entries
63+
-----------------------
64+
6265
In the rare case where the labels cannot directly be set on the handles, they
6366
can also be directly passed to :func:`legend`::
6467
@@ -68,6 +71,17 @@
6871
ax.legend([line_up, line_down], ['Line Up', 'Line Down'])
6972
7073
74+
If the handles are not directly accessible, for example when using some
75+
`Third-party packages <https://matplotlib.org/mpl-third-party/>`_, they can be accessed
76+
as follows. Here we use a dictionary to ensure that we rename the correct entry::
77+
78+
my_map = {'Line Up':'Up', 'Line Down':'Down'}
79+
80+
handles, labels = ax.get_legend_handles_labels()
81+
ax.legend(handles, [my_map[l] for l in labels])
82+
83+
84+
7185
.. _proxy_legend_handles:
7286
7387
Creating artists specifically for adding to the legend (aka. Proxy artists)

0 commit comments

Comments
 (0)
0