Skip to content

Commit 92e608d

Browse files
committed
Merge pull request #3792 from mrkrd/master
ENH : added legend.edgecolor and legend.facecolor rcparams Merged locally to discard a commit + it's reversion.
2 parents 8c4626f + bbf8684 commit 92e608d

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

doc/users/whats_new/rcparams.rst

+7
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ Added "figure.titlesize" and "figure.titleweight" keys to rcParams
1111

1212
Two new keys were added to rcParams to control the default font size and weight
1313
used by the figure title (as emitted by ``pyplot.suptitle()``).
14+
15+
16+
17+
Added "legend.facecolor" and "legend.edgecolor" keys to rcParams
18+
````````````````````````````````````````````````````````````````
19+
20+
The new keys control colors (background and edge) of legend patches.

lib/matplotlib/legend.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,20 @@ def __init__(self, parent, handles, labels,
346346
# We use FancyBboxPatch to draw a legend frame. The location
347347
# and size of the box will be updated during the drawing time.
348348

349+
if rcParams["legend.facecolor"] is None:
350+
facecolor = rcParams["axes.facecolor"]
351+
else:
352+
facecolor = rcParams["legend.facecolor"]
353+
354+
if rcParams["legend.edgecolor"] is None:
355+
edgecolor = rcParams["axes.edgecolor"]
356+
else:
357+
edgecolor = rcParams["legend.edgecolor"]
358+
349359
self.legendPatch = FancyBboxPatch(
350360
xy=(0.0, 0.0), width=1., height=1.,
351-
facecolor=rcParams["axes.facecolor"],
352-
edgecolor=rcParams["axes.edgecolor"],
361+
facecolor=facecolor,
362+
edgecolor=edgecolor,
353363
mutation_scale=self._fontsize,
354364
snap=True
355365
)

lib/matplotlib/mpl-data/stylelib/ggplot.mplstyle

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ axes.color_cycle: E24A33, 348ABD, 988ED5, 777777, FBC15E, 8EBA42, FFB5B8
2525
# 8EBA42 : green
2626
# FFB5B8 : pink
2727

28+
legend.facecolor: white
29+
legend.edgecolor: white
30+
2831
xtick.color: 555555
2932
xtick.direction: out
3033

@@ -36,4 +39,3 @@ grid.linestyle: - # solid line
3639

3740
figure.facecolor: white
3841
figure.edgecolor: 0.50
39-

lib/matplotlib/rcsetup.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,16 @@ def __call__(self, s):
227227

228228
def validate_color(s):
229229
'return a valid color arg'
230-
try:
231-
if s.lower() == 'none':
232-
return 'None'
233-
except AttributeError:
234-
pass
230+
if s in (None, 'none', 'None'):
231+
return None
232+
235233
if is_color_like(s):
236234
return s
235+
237236
stmp = '#' + s
238237
if is_color_like(stmp):
239238
return stmp
239+
240240
# If it is still valid, it must be a tuple.
241241
colorarg = s
242242
msg = ''
@@ -662,6 +662,9 @@ def __call__(self, s):
662662
# the relative size of legend markers vs. original
663663
'legend.markerscale': [1.0, validate_float],
664664
'legend.shadow': [False, validate_bool],
665+
'legend.facecolor': [None, validate_color], # background color; white
666+
'legend.edgecolor': [None, validate_color], # edge color; black
667+
665668

666669
## tick properties
667670
'xtick.major.size': [4, validate_float], # major xtick size in points

lib/matplotlib/tests/test_legend.py

+29
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,35 @@ def test_legend_stackplot():
246246
ax.legend(loc=0)
247247

248248

249+
@cleanup
250+
def _test_rcparams_helper(test_rcparams, facecolor_target, edgecolor_target):
251+
with mpl.rc_context(test_rcparams):
252+
fig, ax = plt.subplots(1, 1)
253+
t = np.linspace(0, 2*np.pi)
254+
ax.plot(t, np.sin(t), label='sin')
255+
ax.plot(t, np.cos(t), label='cos')
256+
leg = ax.legend()
257+
258+
assert_equal(mpl.colors.colorConverter.to_rgba(facecolor_target),
259+
leg.legendPatch.get_facecolor())
260+
261+
assert_equal(mpl.colors.colorConverter.to_rgba(edgecolor_target),
262+
leg.legendPatch.get_edgecolor())
263+
264+
265+
def test_rcparams_():
266+
test_vals = [({}, mpl.rcParams['axes.facecolor'],
267+
mpl.rcParams['axes.edgecolor']),
268+
({'axes.facecolor': 'r', 'axes.edgecolor': 'c'}, 'r', 'c'),
269+
({'axes.facecolor': 'r', 'axes.edgecolor': 'c',
270+
'legend.facecolor': 'w', 'legend.edgecolor': 'k'},
271+
'w', 'k'),
272+
]
273+
274+
for rc_dict, face, edge in test_vals:
275+
yield _test_rcparams_helper, rc_dict, face, edge
276+
277+
249278
if __name__ == '__main__':
250279
import nose
251280
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

matplotlibrc.template

+11-7
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ backend : %(backend)s
256256
# separator in the fr_FR locale.
257257
#axes.formatter.use_mathtext : False # When True, use mathtext for scientific
258258
# notation.
259-
#axes.formatter.useoffset : True # If True, the tick label formatter
260-
# will default to labeling ticks relative
261-
# to an offset when the data range is very
262-
# small compared to the minimum absolute
259+
#axes.formatter.useoffset : True # If True, the tick label formatter
260+
# will default to labeling ticks relative
261+
# to an offset when the data range is very
262+
# small compared to the minimum absolute
263263
# value of the data.
264264

265265
#axes.unicode_minus : True # use unicode for the minus symbol
@@ -307,7 +307,7 @@ backend : %(backend)s
307307
### Legend
308308
#legend.fancybox : False # if True, use a rounded box for the
309309
# legend, else a rectangle
310-
#legend.isaxes : True
310+
#legend.isaxes : True # this option is internally ignored
311311
#legend.numpoints : 2 # the number of points in the legend line
312312
#legend.fontsize : large
313313
#legend.borderpad : 0.5 # border whitespace in fontsize units
@@ -323,6 +323,10 @@ backend : %(backend)s
323323
#legend.frameon : True # whether or not to draw a frame around legend
324324
#legend.framealpha : 1.0 # opacity of of legend frame
325325
#legend.scatterpoints : 3 # number of scatter points
326+
#legend.facecolor : None # legend background color (when None inherits from axes.facecolor)
327+
#legend.edgecolor : None # legend edge color (when None inherits from axes.facecolor)
328+
329+
326330

327331
### FIGURE
328332
# See http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure
@@ -402,7 +406,7 @@ backend : %(backend)s
402406
#savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
403407
#savefig.directory : ~ # default directory in savefig dialog box,
404408
# leave empty to always use current working directory
405-
#savefig.transparent : False # setting that controls whether figures are saved with a
409+
#savefig.transparent : False # setting that controls whether figures are saved with a
406410
# transparent background by default
407411

408412
# tk backend params
@@ -492,5 +496,5 @@ backend : %(backend)s
492496
# $PATH is searched
493497
#animation.mencoder_args: '' # Additional arguments to pass to mencoder
494498
#animation.convert_path: 'convert' # Path to ImageMagick's convert binary.
495-
# On Windows use the full path since convert
499+
# On Windows use the full path since convert
496500
# is also the name of a system tool.

0 commit comments

Comments
 (0)