Skip to content

Commit 2c170f4

Browse files
authored
Merge pull request #18106 from timhoffm/legend-labels
Copy docstring description from Axes.legend() to Figure.legend()
2 parents a0b9fe3 + 88c8c83 commit 2c170f4

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ def legend(self, *args, **kwargs):
323323
legend(labels)
324324
legend(handles, labels)
325325
326-
The call signatures correspond to three different ways how to use
327-
this method.
326+
The call signatures correspond to these three different ways to use
327+
this method:
328328
329329
**1. Automatic detection of elements to be shown in the legend**
330330
@@ -335,7 +335,7 @@ def legend(self, *args, **kwargs):
335335
them either at artist creation or by calling the
336336
:meth:`~.Artist.set_label` method on the artist::
337337
338-
line, = ax.plot([1, 2, 3], label='Inline label')
338+
ax.plot([1, 2, 3], label='Inline label')
339339
ax.legend()
340340
341341
or::
@@ -360,7 +360,7 @@ def legend(self, *args, **kwargs):
360360
ax.plot([1, 2, 3])
361361
ax.legend(['A simple line'])
362362
363-
Note: This way of using is discouraged, because the relation between
363+
Note: This call signature is discouraged, because the relation between
364364
plot elements and labels is only implicit by their order and can
365365
easily be mixed up.
366366
@@ -371,7 +371,7 @@ def legend(self, *args, **kwargs):
371371
to pass an iterable of legend artists followed by an iterable of
372372
legend labels respectively::
373373
374-
legend((line1, line2, line3), ('label1', 'label2', 'label3'))
374+
ax.legend([line1, line2, line3], ['label1', 'label2', 'label3'])
375375
376376
Parameters
377377
----------
@@ -398,6 +398,10 @@ def legend(self, *args, **kwargs):
398398
----------------
399399
%(_legend_kw_doc)s
400400
401+
See Also
402+
--------
403+
.Figure.legend
404+
401405
Notes
402406
-----
403407
Some artists are not supported by this function. See

lib/matplotlib/figure.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,23 +1891,62 @@ def legend(self, *args, **kwargs):
18911891
"""
18921892
Place a legend on the figure.
18931893
1894-
To make a legend from existing artists on every axes::
1894+
Call signatures::
1895+
1896+
legend()
1897+
legend(labels)
1898+
legend(handles, labels)
1899+
1900+
The call signatures correspond to these three different ways to use
1901+
this method:
1902+
1903+
**1. Automatic detection of elements to be shown in the legend**
1904+
1905+
The elements to be added to the legend are automatically determined,
1906+
when you do not pass in any extra arguments.
1907+
1908+
In this case, the labels are taken from the artist. You can specify
1909+
them either at artist creation or by calling the
1910+
:meth:`~.Artist.set_label` method on the artist::
1911+
1912+
ax.plot([1, 2, 3], label='Inline label')
1913+
fig.legend()
1914+
1915+
or::
1916+
1917+
line, = ax.plot([1, 2, 3])
1918+
line.set_label('Label via method')
1919+
fig.legend()
18951920
1896-
legend()
1921+
Specific lines can be excluded from the automatic legend element
1922+
selection by defining a label starting with an underscore.
1923+
This is default for all artists, so calling `.Figure.legend` without
1924+
any arguments and without setting the labels manually will result in
1925+
no legend being drawn.
18971926
1898-
To make a legend for a list of lines and labels::
18991927
1900-
legend(
1901-
(line1, line2, line3),
1902-
('label1', 'label2', 'label3'),
1903-
loc='upper right')
1928+
**2. Labeling existing plot elements**
19041929
1905-
These can also be specified by keyword::
1930+
To make a legend for all artists on all Axes, call this function with
1931+
an iterable of strings, one for each legend item. For example::
19061932
1907-
legend(
1908-
handles=(line1, line2, line3),
1909-
labels=('label1', 'label2', 'label3'),
1910-
loc='upper right')
1933+
fig, (ax1, ax2) = plt.subplots(1, 2)
1934+
ax1.plot([1, 3, 5], color='blue')
1935+
ax2.plot([2, 4, 6], color='red')
1936+
fig.legend(['the blues', 'the reds'])
1937+
1938+
Note: This call signature is discouraged, because the relation between
1939+
plot elements and labels is only implicit by their order and can
1940+
easily be mixed up.
1941+
1942+
1943+
**3. Explicitly defining the elements in the legend**
1944+
1945+
For full control of which artists have a legend entry, it is possible
1946+
to pass an iterable of legend artists followed by an iterable of
1947+
legend labels respectively::
1948+
1949+
fig.legend([line1, line2, line3], ['label1', 'label2', 'label3'])
19111950
19121951
Parameters
19131952
----------
@@ -1934,6 +1973,10 @@ def legend(self, *args, **kwargs):
19341973
----------------
19351974
%(_legend_kw_doc)s
19361975
1976+
See Also
1977+
--------
1978+
.Axes.legend
1979+
19371980
Notes
19381981
-----
19391982
Some artists are not supported by this function. See

0 commit comments

Comments
 (0)