8000 Merge pull request #10715 from jklymak/enh-rc-legend-title-fontsize · matplotlib/matplotlib@dd5f241 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd5f241

Browse files
authored
Merge pull request #10715 from jklymak/enh-rc-legend-title-fontsize
ENH: add title_fontsize to legend
2 parents d0ffbfd + fcdf8c3 commit dd5f241

File tree

3 files changed

+26
-2
lines changed

3 files changed

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

lib/matplotlib/legend.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
232232
title : str or None
233233
The legend's title. Default is no title (``None``).
234234
235+
title_fontsize: str or None
236+
The fontsize of the legend's title. Default is the default fontsize.
237+
235238
borderpad : float or None
236239
The fractional whitespace inside the legend border.
237240
Measured in font-size units.
@@ -333,7 +336,7 @@ def __init__(self, parent, handles, labels,
333336
# box, none use rc
334337
shadow=None,
335338
title=None, # set a title for the legend
336-
339+
title_fontsize=None, # set to ax.fontsize if None
337340
framealpha=None, # set frame alpha
338341
edgecolor=None, # frame patch edgecolor
339342
facecolor=None, # frame patch facecolor
@@ -539,7 +542,12 @@ def __init__(self, parent, handles, labels,
539542
self.get_frame().set_alpha(framealpha)
540543

541544
self._loc = loc
542-
self.set_title(title)
545+
# figure out title fontsize:
546+
if title_fontsize is not None:
547+
tprop = FontProperties(size=title_fontsize)
548+
else:
549+
tprop = None
550+
self.set_title(title, prop=tprop)
543551
self._last_fontsize_points = self._fontsize
544552
self._draggable = None
545553

lib/matplotlib/tests/test_legend.py

Lines 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

0 commit comments

Comments
 (0)
0