Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2216de0

Browse files
committed
Add legend.labelcolor in rcParams
1 parent 93e716d commit 2216de0

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
New rcParams for ledend: set legend labelcolor globaly
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The new :rc:`legend.labelcolor` allows toggling valid color string(for example, 'black'),
5+
versus choosing to match the color of the line of marker using 'linecolor',
6+
'markerfacecolor'(or 'mfc'), or 'markeredgecolor' (or 'mec').
7+
8+
.. plot::
9+
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
13+
# Make some fake data.
14+
a = np.arange(0, 3, .02)
15+
c = np.exp(a)
16+
d = c[::-1]
17+
plt.rcParams['legend.labelcolor'] = 'linecolor'
18+
19+
20+
fig, ax = plt.subplots()
21+
ax.plot(a, c, 'g--', label='Model length')
22+
ax.plot(a, d, 'r:', label='Data length')
23+
24+
legend = ax.legend()
25+
26+
plt.show()

lib/matplotlib/legend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ def __init__(self, parent, handles, labels,
544544
'mec': ['get_markeredgecolor', 'get_edgecolor'],
545545
}
546546
if labelcolor is None:
547-
pass
548-
elif isinstance(labelcolor, str) and labelcolor in color_getters:
547+
labelcolor = mpl.rcParams['legend.labelcolor']
548+
if isinstance(labelcolor, str) and labelcolor in color_getters:
549549
getter_names = color_getters[labelcolor]
550550
for handle, text in zip(self.legendHandles, self.texts):
551551
for getter_name in getter_names:

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@
530530
#legend.scatterpoints: 1 # number of scatter points
531531
#legend.markerscale: 1.0 # the relative size of legend markers vs. original
532532
#legend.fontsize: medium
533+
#legend.labelcolor: black
533534
#legend.title_fontsize: None # None sets to the same as the default axes.
534535

535536
## Dimensions as fraction of font size:

lib/matplotlib/rcsetup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,17 @@ def validate_color_for_prop_cycle(s):
289289
return validate_color(s)
290290

291291

292+
def validate_color_or_linecolor(s):
293+
if cbook._str_equal(s, 'linecolor'):
294+
return s
295+
elif cbook._str_equal(s, 'mfc') or cbook._str_equal(s, 'markerfacecolor'):
296+
return 'markerfacecolor'
297+
elif cbook._str_equal(s, 'mec') or cbook._str_equal(s, 'markeredgecolor'):
298+
return 'markeredgecolor'
299+
300+
return validate_color(s)
301+
302+
292303
def validate_color(s):
293304
"""Return a valid color arg."""
294305
if isinstance(s, str):
@@ -1017,6 +1028,7 @@ def _convert_validator_spec(key, conv):
10171028
"legend.scatterpoints": validate_int,
10181029
"legend.fontsize": validate_fontsize,
10191030
"legend.title_fontsize": validate_fontsize_None,
1031+
"legend.labelcolor": validate_color_or_linecolor, #color of the legend
10201032
# the relative size of legend markers vs. original
10211033
"legend.markerscale": validate_float,
10221034
"legend.shadow": validate_bool,

lib/matplotlib/tests/test_legend.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,58 @@ def test_legend_labelcolor_markerfacecolor():
643643
assert mpl.colors.same_color(text.get_color(), color)
644644

645645

646+
def test_legend_labelcolor_rcparam_single():
647+
# test the rcParams legend.labelcolor for a single color
648+
fig, ax = plt.subplots()
649+
ax.plot(np.arange(10), np.arange(10)*1, label='#1')
650+
ax.plot(np.arange(10), np.arange(10)*2, label='#2')
651+
ax.plot(np.arange(10), np.arange(10)*3, label='#3')
652+
653+
mpl.rcParams['legend.labelcolor'] = 'r'
654+
leg = ax.legend()
655+
for text in leg.get_texts():
656+
assert mpl.colors.same_color(text.get_color(), 'red')
657+
658+
659+
def test_legend_labelcolor_rcparam_linecolor():
660+
# test the rcParams legend.labelcolor for a linecolor
661+
fig, ax = plt.subplots()
662+
ax.plot(np.arange(10), np.arange(10)*1, label='#1', color='r')
663+
ax.plot(np.arange(10), np.arange(10)*2, label='#2', color='g')
664+
ax.plot(np.arange(10), np.arange(10)*3, label='#3', color='b')
665+
666+
mpl.rcParams['legend.labelcolor'] = 'linecolor'
667+
leg = ax.legend()
668+
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
669+
assert mpl.colors.same_color(text.get_color(), color)
670+
671+
672+
def test_legend_labelcolor_rcparam_markeredgecolor():
673+
# test the labelcolor for labelcolor='markeredgecolor'
674+
fig, ax = plt.subplots()
675+
ax.plot(np.arange(10), np.arange(10)*1, label='#1', markeredgecolor='r')
676+
ax.plot(np.arange(10), np.arange(10)*2, label='#2', markeredgecolor='g')
677+
ax.plot(np.arange(10), np.arange(10)*3, label='#3', markeredgecolor='b')
678+
679+
mpl.rcParams['legend.labelcolor'] = 'markeredgecolor'
680+
leg = ax.legend()
681+
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
682+
assert mpl.colors.same_color(text.get_color(), color)
683+
684+
685+
def test_legend_labelcolor_rcparam_markerfacecolor():
686+
# test the labelcolor for labelcolor='markeredgecolor'
687+
fig, ax = plt.subplots()
688+
ax.plot(np.arange(10), np.arange(10)*1, label='#1', markerfacecolor='r')
689+
ax.plot(np.arange(10), np.arange(10)*2, label='#2', markerfacecolor='g')
690+
ax.plot(np.arange(10), np.arange(10)*3, label='#3', markerfacecolor='b')
691+
692+
mpl.rcParams['legend.labelcolor'] = 'markerfacecolor'
693+
leg = ax.legend()
694+
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
695+
assert mpl.colors.same_color(text.get_color(), color)
696+
697+
646698
def test_get_set_draggable():
647699
legend = plt.legend()
648700
assert not legend.get_draggable()

lib/matplotlib/tests/test_rcparams.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
validate_bool,
1919
validate_color,
2020
validate_colorlist,
21+
validate_color_or_linecolor,
2122
validate_cycler,
2223
validate_float,
2324
validate_fontweight,
@@ -330,6 +331,17 @@ def generate_validator_testcases(valid):
330331
('(0, 1, "0.5")', ValueError), # last one not a float
331332
),
332333
},
334+
{'validator': validate_color_or_linecolor,
335+
'success': (('linecolor', 'linecolor'),
336+
('markerfacecolor', 'markerfacecolor'),
337+
('mfc', 'markerfacecolor'),
338+
('markeredgecolor', 'markeredgecolor'),
339+
('mec', 'markeredgecolor')
340+
),
341+
'fail':(('line', ValueError),
342+
('marker', ValueError)
343+
)
344+
},
333345
{'validator': validate_hist_bins,
334346
'success': (('auto', 'auto'),
335347
('fd', 'fd'),

0 commit comments

Comments
 (0)