File tree Expand file tree Collapse file tree 3 files changed +37
-35
lines changed
Filter options
Expand file tree Collapse file tree 3 files changed +37
-35
lines changed
Original file line number Diff line number Diff line change
1
+ `Figure.legend ` no longer checks for repeated lines to ignore
2
+ -------------------------------------------------------------
3
+
4
+ `matplotlib.Figure.legend ` used to check if a line had the
5
+ same label as an existing legend entry that also had the same line color
6
+ or marker color and didn't add a new entry for that line.
7
+
8
+ This list of conditions was incomplete, didn't handle RGB tupples,
9
+ didn't handle linewidths or linestyles etc.
10
+
11
+ This logic did not exist in `Axes.legend `. It was included (erroneously)
12
+ in Matplotlib 2.1.1 when the legend argument parsing was unified
13
+ [#9324](https://github.com/matplotlib/matplotlib/pull/9324). This change
14
+ removes that check again.
15
+
16
+ This logic has also been dropped from `.Figure.legend `, where it
17
+ was previously undocumented. Repeated
18
+ lines with the same label will now each have an entry in the legend. If
19
+ you do not want the duplicate entries, don't add a label to the line, or
20
+ prepend the label with an underscore.
Original file line number Diff line number Diff line change @@ -1346,43 +1346,9 @@ def _get_legend_handles_labels(axs, legend_handler_map=None):
1346
1346
handles = []
1347
1347
labels = []
1348
1348
1349
- def _in_handles (h , l ):
1350
- # Method to check if we already have a given handle and label.
1351
- # Consider two handles to be the same if they share a label,
1352
- # color, facecolor, and edgecolor.
1353
-
1354
- # Loop through each handle and label already collected
1355
- for f_h , f_l in zip (handles , labels ):
1356
- if f_l != l :
1357
- continue
1358
- if type (f_h ) != type (h ):
1359
- continue
1360
- try :
1361
- if (colors .to_rgba_array (f_h .get_color ()) !=
1362
- colors .to_rgba_array (h .get_color ())).any ():
1363
- continue
1364
- except AttributeError :
1365
- pass
1366
- try :
1367
- if (colors .to_rgba_array (f_h .get_facecolor ()) !=
1368
- colors .to_rgba_array (h .get_facecolor ())).any ():
1369
- continue
1370
- except AttributeError :
1371
- pass
1372
- try :
1373
- if (colors .to_rgba_array (f_h .get_edgecolor ()) !=
1374
- colors .to_rgba_array (h .get_edgecolor ())).any ():
1375
- continue
1376
- except AttributeError :
1377
- pass
1378
- return True
1379
- return False
1380
-
1381
1349
for handle in _get_legend_handles (axs , legend_handler_map ):
1382
1350
label = handle .get_label ()
1383
- if (label and
1384
- not label .startswith ('_' ) and
1385
- not _in_handles (handle , label )):
1351
+ if (label and not label .startswith ('_' )):
1386
1352
handles .append (handle )
1387
1353
labels .append (label )
1388
1354
return handles , labels
Original file line number Diff line number Diff line change 16
16
import matplotlib .transforms as mtransforms
17
17
import matplotlib .collections as mcollections
18
18
from matplotlib .legend_handler import HandlerTuple
19
+ import matplotlib .legend as mlegend
19
20
import inspect
20
21
21
22
@@ -423,6 +424,21 @@ def test_nanscatter():
423
424
ax .grid (True )
424
425
425
426
427
+ def test_legend_repeatcheckok ():
428
+ fig , ax = plt .subplots ()
429
+ ax .scatter (0.0 , 1.0 , color = 'k' , marker = 'o' , label = 'test' )
430
+ ax .scatter (0.5 , 0.0 , color = 'r' , marker = 'v' , label = 'test' )
431
+ hl = ax .legend ()
432
+ hand , lab = mlegend ._get_legend_handles_labels ([ax ])
433
+ assert len (lab ) == 2
434
+ fig , ax = plt .subplots ()
435
+ ax .scatter (0.0 , 1.0 , color = 'k' , marker = 'o' , label = 'test' )
436
+ ax .scatter (0.5 , 0.0 , color = 'k' , marker = 'v' , label = 'test' )
437
+ hl = ax .legend ()
438
+ hand , lab = mlegend ._get_legend_handles_labels ([ax ])
439
+ assert len (lab ) == 2
440
+
441
+
426
442
@image_comparison (baseline_images = ['not_covering_scatter' ], extensions = ['png' ])
427
443
def test_not_covering_scatter ():
428
444
colors = ['b' , 'g' , 'r' ]
You can’t perform that action at this time.
0 commit comments