8000 ENH: add title_fontsize to legend · matplotlib/matplotlib@3f551ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f551ea

Browse files
committed
ENH: add title_fontsize to legend
1 parent b5391ce commit 3f551ea

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed
Lines 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/axes/_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ def legend(self, *args, **kwargs):
489489
title : str or None
490490
The legend's title. Default is no title (``None``).
491491
492+
title_fontsize : str, float, or None
493+
The legend title fontsize. Default is ``None` will use
494+
:rc:`legend.title_fontsize`.
495+
492496
borderpad : float or None
493497
The fractional whitespace inside the legend border.
494498
Measured in font-size units.

lib/matplotlib/figure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,10 @@ def legend(self, *args, **kwargs):
16761676
title : str or None
16771677
The legend's title. Default is no title (``None``).
16781678
1679+
title_fontsize : str, float, or None
1680+
The legend title fontsize. Default is ``None` will use
1681+
:rc:`legend.title_fontsize`.
1682+
16791683
borderpad : float or None
16801684
The fractional whitespace inside the legend border.
16811685
Measured in font-size units.

lib/matplotlib/legend.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def __init__(self, parent, handles, labels,
337337
# box, none use rc
338338
shadow=None,
339339
title=None, # set a title for the legend
340-
340+
title_fontsize=None, # set to ax.fontsize if None
341341
framealpha=None, # set frame alpha
342342
edgecolor=None, # frame patch edgecolor
343343
facecolor=None, # frame patch facecolor
@@ -491,6 +491,10 @@ def __init__(self, parent, handles, labels,
491491
title : str or None
492492
The legend's title. Default is no title (``None``).
493493
494+
title_fontsize : str, float, or None
495+
The legend title fontsize. Default is ``None` will use
496+
:rc:`legend.title_fontsize`.
497+
494498
borderpad : float or None
495499
The fractional whitespace inside the legend border.
496500
Measured in font-size units.
@@ -710,7 +714,12 @@ def __init__(self, parent, handles, labels,
710714
self.get_frame().set_alpha(framealpha)
711715

712716
self._loc = loc
713-
self.set_title(title)
717+
# figure out title fontsize:
718+
if title_fontsize is None:
719+
title_fontsize = rcParams['legend.title_fontsize']
720+
print(self.prop)
721+
tprop = FontProperties(size=title_fontsize)
722+
self.set_title(title, prop=tprop)
714723
self._last_fontsize_points = self._fontsize
715724
self._draggable = None
716725

lib/matplotlib/rcsetup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,13 @@ def validate_aspect(s):
434434
raise ValueError('not a valid aspect specification')
435435

436436

437+
def validate_fontsize_None(s):
438+
if s is None:
439+
return s
440+
else:
441+
return validate_fontsize(s)
442+
443+
437444
def validate_fontsize(s):
438445
fontsizes = ['xx-small', 'x-small', 'small', 'medium', 'large',
439446
'x-large', 'xx-large', 'smaller', 'larger']
@@ -1176,6 +1183,7 @@ def _validate_linestyle(ls):
11761183
# the number of points in the legend line for scatter
11771184
'legend.scatterpoints': [1, validate_int],
11781185
'legend.fontsize': ['medium', validate_fontsize],
1186+
'legend.title_fontsize': [None, validate_fontsize_None],
11791187
# the relative size of legend markers vs. original
11801188
'legend.markerscale': [1.0, validate_float],
11811189
'legend.shadow': [False, validate_bool],

lib/matplotlib/tests/test_legend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,11 @@ def test_legend_title_empty():
522522
leg = ax.legend()
523523
assert leg.get_title().get_text() == ""
524524
assert leg.get_title().get_visible() is False
525+
526+
527+
def test_legend_title_fontsize():
528+
# test the title_fontsize kwarg
529+
fig, ax = plt.subplots()
530+
ax.plot(range(10))
531+
leg = ax.legend(title='Aardvark', title_fontsize=22)
532+
assert leg.get_title().get_fontsize() == 22

matplotlibrc.template

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ backend : $TEMPLATE_BACKEND
327327
## color cycle for plot lines as list of string
328328
## colorspecs: single letter, long name, or web-style hex
329329
## Note the use of string escapes here ('1f77b4', instead of 1f77b4)
330-
## as opposed to the rest of this file.
330+
## as opposed to the rest of this file.
331331
#axes.autolimit_mode : data ## How to scale axes limits to the data.
332332
## Use "data" to use data limits, plus some margin
333333
## Use "round_number" move to the nearest "round" number
@@ -414,6 +414,7 @@ backend : $TEMPLATE_BACKEND
414414
#legend.scatterpoints : 1 ## number of scatter points
415415
#legend.markerscale : 1.0 ## the relative size of legend markers vs. original
416416
#legend.fontsize : medium
417+
#legend.title_fontsize : None ## None sets to the same as the default axes.
417418
## Dimensions as fraction of fontsize:
418419
#legend.borderpad : 0.4 ## border whitespace
419420
#legend.labelspacing : 0.5 ## the vertical space between the legend entries
@@ -451,11 +452,11 @@ backend : $TEMPLATE_BACKEND
451452
## using `tight_layout`
452453
#figure.constrained_layout.use: False ## When True, automatically make plot
453454
## elements fit on the figure. (Not compatible
454-
## with `autolayout`, above).
455-
#figure.constrained_layout.h_pad : 0.04167 ## Padding around axes objects. Float representing
455+
## with `autolayout`, above).
456+
#figure.constrained_layout.h_pad : 0.04167 ## Padding around axes objects. Float representing
456457
#figure.constrained_layout.w_pad : 0.04167 ## inches. Default is 3./72. inches (3 pts)
457-
#figure.constrained_layout.hspace : 0.02 ## Space between subplot groups. Float representing
458-
#figure.constrained_layout.wspace : 0.02 ## a fraction of the subplot widths being separated.
458+
#figure.constrained_layout.hspace : 0.02 ## Space between subplot groups. Float representing
459+
#figure.constrained_layout.wspace : 0.02 ## a fraction of the subplot widths being separated.
459460

460461
#### IMAGES
461462
#image.aspect : equal ## equal | auto | a number
@@ -494,12 +495,12 @@ backend : $TEMPLATE_BACKEND
494495
## It may cause minor artifacts, though.
495496
## A value of 20000 is probably a good
496497
## starting point.
497-
#### PATHS
498+
#### PATHS
498499
#path.simplify : True ## When True, simplify paths by removing "invisible"
499500
## points to reduce file size and increase rendering
500501
## speed
501502
#path.simplify_threshold : 0.111111111111 ## The threshold of similarity below which
502-
## vertices will be removed in the
503+
## vertices will be removed in the
503504
## simplification process
504505
#path.snap : True ## When True, rectilinear axis-aligned paths will be snapped to
505506
## the nearest pixel when certain criteria are met. When False,
@@ -532,8 +533,8 @@ backend : $TEMPLATE_BACKEND
532533
## leave empty to always use current working directory
533534
#savefig.transparent : False ## setting that controls whether figures are saved with a
534535
## transparent background by default
535-
#savefig.frameon : True ## enable frame of figure when saving
536-
#savefig.orientation : portrait ## Orientation of saved figure
536+
#savefig.frameon : True ## enable frame of figure when saving
537+
#savefig.orientation : portrait ## Orientation of saved figure
537538

538539
### tk backend params
539540
#tk.window_focus : False ## Maintain shell focus for TkAgg
@@ -566,7 +567,7 @@ backend : $TEMPLATE_BACKEND
566567
## instead of uuid4
567568
### pgf parameter
568569
#pgf.rcfonts : True
569-
#pgf.preamble :
570+
#pgf.preamble :
570571
#pgf.texsystem : xelatex
571572
#pgf.debug : False
572573

@@ -597,7 +598,7 @@ backend : $TEMPLATE_BACKEND
597598
###ANIMATION settings
598599
#animation.html : none ## How to display the animation as HTML in
599600
## the IPython notebook. 'html5' uses
600-
## HTML5 video tag; 'jshtml' creates a
601+
## HTML5 video tag; 'jshtml' creates a
601602
## Javascript animation
602603
#animation.writer : ffmpeg ## MovieWriter 'backend' to use
603604
#animation.codec : h264 ## Codec to use for writing movie

0 commit comments

Comments
 (0)
0