Skip to content

Commit 1403335

Browse files
committed
textcolor: included tests for markeredgecolor and markerfacecolor
1 parent 6df3f84 commit 1403335

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

doc/users/next_whats_new/2019-12-09-legend-textcolor.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ Textcolor for legend labels
22
---------------------------
33

44
The text color of legend labels can now be set by passing a parameter
5-
``textcolor`` to `~.axes.Axes.legend`. The ``textcolor`` keyword can be
6-
either a single color, which adjusts the color of all of labels. Alternatively,
7-
it can be a list or tuple, allowing the color of the label texts to be set
8-
individually. Finally, it can be set to ``linecolor``, ``markerfacecolor``, or
9-
``markeredgecolor`` so that the label text can be set to the colors of the lines
10-
or markers.
5+
``textcolor`` to `~.axes.Axes.legend`. The ``textcolor`` keyword can be:
6+
7+
* A single color (either a string or RGBA tuple), which adjusts the color of
8+
all of labels.
9+
* A list or tuple, allowing the color of each label text to be set
10+
individually.
11+
* ``linecolor``, which sets the color of each label to match the corresponding
12+
line color.
13+
* ``markerfacecolor``, which sets the color of each label to match the
14+
corresponding marker face color
15+
* ``markeredgecolor``, which sets the color of each label to match the
16+
corresponding marker edge color

lib/matplotlib/legend.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
information.
2222
"""
2323

24+
import itertools
2425
import logging
2526
import time
26-
import itertools
27+
2728
import numpy as np
2829

2930
from matplotlib import rcParams
@@ -178,10 +179,8 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
178179
textcolor : str or list
179180
Sets the color of the text in the legend. Can be a valid color string
180181
(for example, 'red'), or a list of color strings. The textcolor can
181-
also be made to match the color of the line or marker using the following:
182-
line color ('linecolor'),
183-
marker face color ('markerfacecolor' or 'mfc')
184-
marker edge color ('markeredgecolor' or 'mec')
182+
also be made to match the color of the line or marker using 'linecolor',
183+
'markerfacecolor' (or 'mfc'), or 'markeredgecolor' (or 'mec').
185184
186185
numpoints : int, default: :rc:`legend.numpoints`
187186
The number of marker points in the legend when creating a legend
@@ -548,11 +547,11 @@ def __init__(self, parent, handles, labels,
548547
# set the text color
549548

550549
color_getters = { # getter function depends on line or patch
551-
'linecolor': ['get_color', 'get_facecolor'],
550+
'linecolor': ['get_color', 'get_facecolor'],
552551
'markerfacecolor': ['get_markerfacecolor', 'get_facecolor'],
553552
'mfc': ['get_markerfacecolor', 'get_facecolor'],
554553
'markeredgecolor': ['get_markeredgecolor', 'get_edgecolor'],
555-
'mec': ['get_markerfacecolor', 'get_facecolor'],
554+
'mec': ['get_markeredgecolor', 'get_edgecolor'],
556555
}
557556
if textcolor is None:
558557
pass

lib/matplotlib/tests/test_legend.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_legend_textcolor_single():
541541

542542
leg = ax.legend(textcolor='red')
543543
for text in leg.get_texts():
544-
assert (text.get_color() == mpl.colors.to_rgba_array('red')).all
544+
assert mpl.colors.same_color(text.get_color(), 'red')
545545

546546

547547
def test_legend_textcolor_list():
@@ -553,7 +553,7 @@ def test_legend_textcolor_list():
553553

554554
leg = ax.legend(textcolor=['r', 'g', 'b'])
555555
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
556-
assert (text.get_color() == mpl.colors.to_rgba_array(color)).all
556+
assert mpl.colors.same_color(text.get_color(), color)
557557

558558

559559
def test_legend_textcolor_linecolor():
@@ -568,6 +568,30 @@ def test_legend_textcolor_linecolor():
568568
assert text.get_color() == color
569569

570570

571+
def test_legend_textcolor_markeredgecolor():
572+
# test the textcolor for textcolor='markeredgecolor'
573+
fig, ax = plt.subplots()
574+
ax.plot(np.arange(10), np.arange(10)*1, label='#1', markeredgecolor='r')
575+
ax.plot(np.arange(10), np.arange(10)*2, label='#2', markeredgecolor='g')
576+
ax.plot(np.arange(10), np.arange(10)*3, label='#3', markeredgecolor='b')
577+
578+
leg = ax.legend(textcolor='markeredgecolor')
579+
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
580+
assert text.get_color() == color
581+
582+
583+
def test_legend_textcolor_markerfacecolor():
584+
# test the textcolor for textcolor='markerfacecolor'
585+
fig, ax = plt.subplots()
586+
ax.plot(np.arange(10), np.arange(10)*1, label='#1', markerfacecolor='r')
587+
ax.plot(np.arange(10), np.arange(10)*2, label='#2', markerfacecolor='g')
588+
ax.plot(np.arange(10), np.arange(10)*3, label='#3', markerfacecolor='b')
589+
590+
leg = ax.legend(textcolor='markerfacecolor')
591+
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
592+
assert text.get_color() == color
593+
594+
571595
def test_get_set_draggable():
572596
legend = plt.legend()
573597
assert not legend.get_draggable()

0 commit comments

Comments
 (0)