Skip to content

Commit 5598853

Browse files
authored
Merge pull request #6770 from efiring/legend_boundary
STY: make default legend edgecolor gray
2 parents 8751f9b + f8c770e commit 5598853

File tree

6 files changed

+72
-23
lines changed

6 files changed

+72
-23
lines changed

doc/api/api_changes/2016-07-20-EF.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
`Legend` initializers gain edgecolor and facecolor kwargs
2+
``````````````````````````````````````````````````````````
3+
4+
The :class:`~matplotlib.legend.Legend` background patch (or 'frame')
5+
can have its `edgecolor` and `facecolor` determined by the
6+
corresponding keyword arguments to its initializer, or to any of the
7+
methods or functions that call that initializer. If left to
8+
their default values of `None`, their values will be taken from
9+
`rcParams`. The previously-existing `framealpha` kwarg still
10+
controls the alpha transparency of the patch.

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def legend(self, *args, **kwargs):
408408
label
409409
410410
frameon : None or bool
411-
Control whether a frame should be drawn around the legend.
411+
Control whether the legend should be drawn on a patch (frame).
412412
Default is ``None`` which will take the value from the
413413
``legend.frameon`` :data:`rcParam<matplotlib.rcParams>`.
414414
@@ -425,10 +425,24 @@ def legend(self, *args, **kwargs):
425425
``legend.shadow`` :data:`rcParam<matplotlib.rcParams>`.
426426
427427
framealpha : None or float
428-
Control the alpha transparency of the legend's frame.
428+
Control the alpha transparency of the legend's background.
429429
Default is ``None`` which will take the value from the
430430
``legend.framealpha`` :data:`rcParam<matplotlib.rcParams>`.
431431
432+
facecolor : None or "inherit" or a color spec
433+
Control the legend's background color.
434+
Default is ``None`` which will take the value from the
435+
``legend.facecolor`` :data:`rcParam<matplotlib.rcParams>`.
436+
If ``"inherit"``, it will take the ``axes.facecolor``
437+
:data:`rcParam<matplotlib.rcParams>`.
438+
439+
edgecolor : None or "inherit" or a color spec
440+
Control the legend's background patch edge color.
441+
Default is ``None`` which will take the value from the
442+
``legend.edgecolor`` :data:`rcParam<matplotlib.rcParams>`.
443+
If ``"inherit"``, it will take the ``axes.edgecolor``
444+
:data:`rcParam<matplotlib.rcParams>`.
445+
432446
mode : {"expand", None}
433447
If `mode` is set to ``"expand"`` the legend will be horizontally
434448
expanded to fill the axes area (or `bbox_to_anchor` if defines

lib/matplotlib/figure.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,12 +1206,36 @@ def legend(self, handles, labels, *args, **kwargs):
12061206
if *False*, legend marker is placed to the right of the legend
12071207
label
12081208
1209+
*frameon*: [ *None* | bool ]
1210+
Control whether the legend should be drawn on a patch (frame).
1211+
Default is *None* which will take the value from the
1212+
``legend.frameon`` :data:`rcParam<matplotlib.rcParams>`.
1213+
12091214
*fancybox*: [ *None* | *False* | *True* ]
12101215
if *True*, draw a frame with a round fancybox. If *None*, use rc
12111216
12121217
*shadow*: [ *None* | *False* | *True* ]
12131218
If *True*, draw a shadow behind legend. If *None*, use rc settings.
12141219
1220+
*framealpha*: [ *None* | float ]
1221+
Control the alpha transparency of the legend's background.
1222+
Default is *None* which will take the value from the
1223+
``legend.framealpha`` :data:`rcParam<matplotlib.rcParams>`.
1224+
1225+
*facecolor*: [ *None* | "inherit" | a color spec ]
1226+
Control the legend's background color.
1227+
Default is *None* which will take the value from the
1228+
``legend.facecolor`` :data:`rcParam<matplotlib.rcParams>`.
1229+
If ``"inherit"``, it will take the ``axes.facecolor``
1230+
:data:`rcParam<matplotlib.rcParams>`.
1231+
1232+
*edgecolor*: [ *None* | "inherit" | a color spec ]
1233+
Control the legend's background patch edge color.
1234+
Default is *None* which will take the value from the
1235+
``legend.edgecolor`` :data:`rcParam<matplotlib.rcParams>`.
1236+
If ``"inherit"``, it will take the ``axes.edgecolor``
1237+
:data:`rcParam<matplotlib.rcParams>`.
1238+
12151239
*ncol* : integer
12161240
number of columns. default is 1
12171241

lib/matplotlib/legend.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def __init__(self, parent, handles, labels,
180180
title=None, # set a title for the legend
181181

182182
framealpha=None, # set frame alpha
183+
edgecolor=None, # frame patch edgecolor
184+
facecolor=None, # frame patch facecolor
183185

184186
bbox_to_anchor=None, # bbox that the legend will be anchored.
185187
bbox_transform=None, # transform for the bbox
@@ -197,21 +199,20 @@ def __init__(self, parent, handles, labels,
197199
================ ====================================================
198200
Keyword Description
199201
================ ====================================================
200-
loc a location code
202+
loc Location code string, or tuple (see below).
201203
prop the font property
202204
fontsize the font size (used only if prop is not specified)
203205
markerscale the relative size of legend markers vs. original
204-
markerfirst If true, place legend marker to left of label
205-
If false, place legend marker to right of label
206+
markerfirst If True (default), marker is to left of the label.
206207
numpoints the number of points in the legend for line
207208
scatterpoints the number of points in the legend for scatter plot
208209
scatteryoffsets a list of yoffsets for scatter symbols in legend
209-
frameon if True, draw a frame around the legend.
210-
If None, use rc
211-
fancybox if True, draw a frame with a round fancybox.
212-
If None, use rc
213-
shadow if True, draw a shadow behind legend
214-
framealpha If not None, alpha channel for the frame.
210+
frameon If True, draw the legend on a patch (frame).
211+
fancybox If True, draw the frame with a round fancybox.
212+
shadow If True, draw a shadow behind legend.
213+
framealpha Transparency of the frame.
214+
edgecolor Frame edgecolor.
215+
facecolor Frame facecolor.
215216
ncol number of columns
216217
borderpad the fractional whitespace inside the legend border
217218
labelspacing the vertical space between the legend entries
@@ -345,15 +346,15 @@ def __init__(self, parent, handles, labels,
345346
# We use FancyBboxPatch to draw a legend frame. The location
346347
# and size of the box will be updated during the drawing time.
347348

348-
if rcParams["legend.facecolor"] == 'inherit':
349-
facecolor = rcParams["axes.facecolor"]
350-
else:
349+
if facecolor is None:
351350
facecolor = rcParams["legend.facecolor"]
351+
if facecolor == 'inherit':
352+
facecolor = rcParams["axes.facecolor"]
352353

353-
if rcParams["legend.edgecolor"] == 'inherit':
354-
edgecolor = rcParams["axes.edgecolor"]
355-
else:
354+
if edgecolor is None:
356355
edgecolor = rcParams["legend.edgecolor"]
356+
if edgecolor == 'inherit':
357+
edgecolor = rcParams["axes.edgecolor"]
357358

358359
self.legendPatch = FancyBboxPatch(
359360
xy=(0.0, 0.0), width=1., height=1.,

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ def validate_hist_bins(s):
11461146
'legend.markerscale': [1.0, validate_float],
11471147
'legend.shadow': [False, validate_bool],
11481148
'legend.facecolor': ['inherit', validate_color_or_inherit],
1149-
'legend.edgecolor': ['none', validate_color_or_inherit],
1149+
'legend.edgecolor': ['0.8', validate_color_or_inherit],
11501150

11511151
# tick properties
11521152
'xtick.top': [False, validate_bool], # draw ticks on the top side

matplotlibrc.template

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ backend : $TEMPLATE_BACKEND
404404

405405
### Legend
406406
#legend.loc : best
407-
#legend.frameon : True # whether or not to draw a frame around legend
408-
#legend.framealpha : 0.8 # legend frame transparency
407+
#legend.frameon : True # if True, draw the legend on a background patch
408+
#legend.framealpha : 0.8 # legend patch transparency
409409
#legend.facecolor : inherit # inherit from axes.facecolor; or color spec
410-
#legend.edgecolor : none
410+
#legend.edgecolor : 0.8 # background patch boundary color
411411
#legend.fancybox : True # if True, use a rounded box for the
412-
# legend, else a rectangle
413-
#legend.shadow : False
412+
# legend background, else a rectangle
413+
#legend.shadow : False # if True, give background a shadow effect
414414
#legend.numpoints : 1 # the number of marker points in the legend line
415415
#legend.scatterpoints : 1 # number of scatter points
416416
#legend.markerscale : 1.0 # the relative size of legend markers vs. original

0 commit comments

Comments
 (0)