@@ -191,10 +191,11 @@ def legend(self, *args, **kwargs):
191
191
Call signatures::
192
192
193
193
legend()
194
- legend(labels)
195
194
legend(handles, labels)
195
+ legend(handles=handles)
196
+ legend(labels)
196
197
197
- The call signatures correspond to these three different ways to use
198
+ The call signatures correspond to the following different ways to use
198
199
this method:
199
200
200
201
**1. Automatic detection of elements to be shown in the legend**
@@ -222,27 +223,40 @@ def legend(self, *args, **kwargs):
222
223
no legend being drawn.
223
224
224
225
225
- **2. Labeling existing plot elements **
226
+ **2. Explicitly listing the artists and labels in the legend **
226
227
227
- To make a legend for lines which already exist on the Axes
228
- (via plot for instance), simply call this function with an iterable
229
- of strings, one for each legend item. For example ::
228
+ For full control of which artists have a legend entry, it is possible
229
+ to pass an iterable of legend artists followed by an iterable of
230
+ legend labels respectively ::
230
231
231
- ax.plot([1, 2, 3])
232
- ax.legend(['A simple line'])
232
+ ax.legend([line1, line2, line3], ['label1', 'label2', 'label3'])
233
233
234
- Note: This call signature is discouraged, because the relation between
235
- plot elements and labels is only implicit by their order and can
236
- easily be mixed up.
237
234
235
+ **3. Explicitly listing the artists in the legend**
238
236
239
- **3. Explicitly defining the elements in the legend**
237
+ This is similar to 2, but the labels are taken from the artists'
238
+ label properties. Example::
240
239
241
- For full control of which artists have a legend entry, it is possible
242
- to pass an iterable of legend artists followed by an iterable of
243
- legend labels respectively::
240
+ line1, = ax.plot([1, 2, 3], label='label1')
241
+ line2, = ax.plot([1, 2, 3], label='label2')
242
+ ax.legend(handles=[line1, line2])
243
+
244
+
245
+ **4. Labeling existing plot elements**
246
+
247
+ .. admonition:: Discouraged
248
+
249
+ This call signature is discouraged, because the relation between
250
+ plot elements and labels is only implicit by their order and can
251
+ easily be mixed up.
252
+
253
+ To make a legend for all artists on an Axes, call this function with
254
+ an iterable of strings, one for each legend item. For example::
255
+
256
+ ax.plot([1, 2, 3])
257
+ ax.plot([5, 6, 7])
258
+ ax.legend(['First line', 'Second line'])
244
259
245
- ax.legend([line1, line2, line3], ['label1', 'label2', 'label3'])
246
260
247
261
Parameters
248
262
----------
0 commit comments