Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 156131c

Browse filesBrowse files
committed
ENH: add title_fontsize to legend
1 parent e4ce92f commit 156131c
Copy full SHA for 156131c

File tree

Expand file treeCollapse file tree

5 files changed

+32
-2
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+32
-2
lines changed
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Legend now has a title_fontsize kwarg (and rcParam)
2+
---------------------------------------------------
3+
4+
The title for a `.Figure.legend` and `.Axes.legend` can now have its
5+
fontsize set via the ``title_fontsize`` kwarg. There is also a new
6+
:rc:`legend.title_fontsize`. Both default to ``None``, which means
7+
the legend title will have the same fontsize as the axes default fontsize
8+
(*not* the legend fontsize, set by the ``fontsize`` kwarg or
9+
:rc:`legend.fontsize`).

‎lib/matplotlib/legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend.py
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def __init__(self, parent, handles, labels,
333333
# box, none use rc
334334
shadow=None,
335335
title=None, # set a title for the legend
336-
336+
title_fontsize=None, # set to ax.fontsize if None
337337
framealpha=None, # set frame alpha
338338
edgecolor=None, # frame patch edgecolor
339339
facecolor=None, # frame patch facecolor
@@ -539,7 +539,11 @@ def __init__(self, parent, handles, labels,
539539
self.get_frame().set_alpha(framealpha)
540540

541541
self._loc = loc
542-
self.set_title(title)
542+
# figure out title fontsize:
543+
if title_fontsize is None:
544+
title_fontsize = rcParams['legend.title_fontsize']
545+
tprop = FontProperties(size=title_fontsize)
546+
self.set_title(title, prop=tprop)
543547
self._last_fontsize_points = self._fontsize
544548
self._draggable = None
545549

‎lib/matplotlib/rcsetup.py

Copy file name to clipboardExpand all lines: lib/matplotlib/rcsetup.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,13 @@ def validate_aspect(s):
420420
raise ValueError('not a valid aspect specification')
421421

422422

423+
def validate_fontsize_None(s):
424+
if s is None or s == 'None':
425+
return None
426+
else:
427+
return validate_fontsize(s)
428+
429+
423430
def validate_fontsize(s):
424431
fontsizes = ['xx-small', 'x-small', 'small', 'medium', 'large',
425432
'x-large', 'xx-large', 'smaller', 'larger']
@@ -1226,6 +1233,7 @@ def _validate_linestyle(ls):
12261233
# the number of points in the legend line for scatter
12271234
'legend.scatterpoints': [1, validate_int],
12281235
'legend.fontsize': ['medium', validate_fontsize],
1236+
'legend.title_fontsize': [None, validate_fontsize_None],
12291237
# the relative size of legend markers vs. original
12301238
'legend.markerscale': [1.0, validate_float],
12311239
'legend.shadow': [False, validate_bool],

‎lib/matplotlib/tests/test_legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_legend.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,11 @@ def test_legend_proper_window_extent():
502502
leg = ax.legend()
503503
x02 = leg.get_window_extent(fig.canvas.get_renderer()).x0
504504
assert pytest.approx(x01*2, 0.1) == x02
505+
506+
507+
def test_legend_title_fontsize():
508+
# test the title_fontsize kwarg
509+
fig, ax = plt.subplots()
510+
ax.plot(range(10))
511+
leg = ax.legend(title='Aardvark', title_fontsize=22)
512+
assert leg.get_title().get_fontsize() == 22

‎matplotlibrc.template

Copy file name to clipboardExpand all lines: matplotlibrc.template
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ backend : $TEMPLATE_BACKEND
415415
#legend.scatterpoints : 1 ## number of scatter points
416416
#legend.markerscale : 1.0 ## the relative size of legend markers vs. original
417417
#legend.fontsize : medium
418+
#legend.title_fontsize : None ## None sets to the same as the default axes.
418419
## Dimensions as fraction of fontsize:
419420
#legend.borderpad : 0.4 ## border whitespace
420421
#legend.labelspacing : 0.5 ## the vertical space between the legend entries

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.