8000 Merge pull request #19304 from smartlixx/add-lgend-title-properties · matplotlib/matplotlib@5799a5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5799a5c

Browse files
authored
Merge pull request #19304 from smartlixx/add-lgend-title-properties
Add legend title font properties
2 parents 642c6d0 + f5d44e0 commit 5799a5c

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Font properties of legend title are configurable
2+
------------------------------------------------
3+
4+
Title's font properties can be set via the *title_fontproperties* keyword
5+
argument, for example:
6+
7+
.. plot::
8+
9+
fig, ax = plt.subplots(figsize=(4, 3))
10+
ax.plot(range(10),label='point')
11+
ax.legend(title='Points', title_fontproperties={'family': 'serif', 'size': 20})

lib/matplotlib/legend.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,17 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
237237
title : str or None
238238
The legend's title. Default is no title (``None``).
239239
240+
title_fontproperties : None or `matplotlib.font_manager.FontProperties` or dict
241+
The font properties of the legend's title. If None (default), the
242+
*title_fontsize* argument will be used if present; if *title_fontsize* is
243+
also None, the current :rc:`legend.title_fontsize` will be used.
244+
240245
title_fontsize : int or {'xx-small', 'x-small', 'small', 'medium', 'large', \
241246
'x-large', 'xx-large'}, default: :rc:`legend.title_fontsize`
242247
The font size of the legend's title.
248+
Note: This cannot be combined with *title_fontproperties*. If you want
249+
to set the fontsize alongside other font properties, use the *size*
250+
parameter in *title_fontproperties*.
243251
244252
borderpad : float, default: :rc:`legend.borderpad`
245253
The fractional whitespace inside the legend border, in font-size units.
@@ -332,6 +340,7 @@ def __init__(self, parent, handles, labels,
332340
bbox_transform=None, # transform for the bbox
333341
frameon=None, # draw frame
334342
handler_map=None,
343+
title_fontproperties=None, # properties for the legend title
335344
):
336345
"""
337346
Parameters
@@ -506,11 +515,23 @@ def __init__(self, parent, handles, labels,
506515
self._set_loc(loc)
507516
self._loc_used_default = tmp # ignore changes done by _set_loc
508517

509-
# figure out title fontsize:
510-
if title_fontsize is None:
511-
title_fontsize = mpl.rcParams['legend.title_fontsize']
512-
tprop = FontProperties(size=title_fontsize)
513-
self.set_title(title, prop=tprop)
518+
# figure out title font properties:
519+
if title_fontsize is not None and title_fontproperties is not None:
520+
raise ValueError(
521+
"title_fontsize and title_fontproperties can't be specified "
522+
"at the same time. Only use one of them. ")
523+
title_prop_fp = FontProperties._from_any(title_fontproperties)
524+
if isinstance(title_fontproperties, dict):
525+
if "size" not in title_fontproperties:
526+
title_fontsize = mpl.rcParams["legend.title_fontsize"]
527+
title_prop_fp.set_size(title_fontsize)
528+
elif title_fontsize is not None:
529+
title_prop_fp.set_size(title_fontsize)
530+
elif not isinstance(title_fontproperties, FontProperties):
531+
title_fontsize = mpl.rcParams["legend.title_fontsize"]
532+
title_prop_fp.set_size(title_fontsize)
533+
534+
self.set_title(title, prop=title_prop_fp)
514535
self._draggable = None
515536

516537
# set the text color

lib/matplotlib/tests/test_legend.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from matplotlib.legend_handler import HandlerTuple
1414
import matplotlib.legend as mlegend
1515
from matplotlib import rc_context
16+
from matplotlib.font_manager import FontProperties
1617

1718

1819
def test_legend_ordereddict():
@@ -544,12 +545,42 @@ def test_window_extent_cached_renderer():
544545
leg2.get_window_extent()
545546

546547

547-
def test_legend_title_fontsize():
548+
def test_legend_title_fontprop_fontsize():
548549
# test the title_fontsize kwarg
549-
fig, ax = plt.subplots()
550-
ax.plot(range(10))
551-
leg = ax.legend(title='Aardvark', title_fontsize=22)
552-
assert leg.get_title().get_fontsize() == 22
550+
plt.plot(range(10))
551+
with pytest.raises(ValueError):
552+
plt.legend(title='Aardvark', title_fontsize=22,
553+
title_fontproperties={ 67E6 9;family': 'serif', 'size': 22})
554+
555+
leg = plt.legend(title='Aardvark', title_fontproperties=FontProperties(
556+
family='serif', size=22))
557+
assert leg.get_title().get_size() == 22
558+
559+
fig, axes = plt.subplots(2, 3, figsize=(10, 6))
560+
axes = axes.flat
561+
axes[0].plot(range(10))
562+
leg0 = axes[0].legend(title='Aardvark', title_fontsize=22)
563+
assert leg0.get_title().get_fontsize() == 22
564+
axes[1].plot(range(10))
565+
leg1 = axes[1].legend(title='Aardvark',
566+
title_fontproperties={'family': 'serif', 'size': 22})
567+
assert leg1.get_title().get_fontsize() == 22
568+
axes[2].plot(range(10))
569+
mpl.rcParams['legend.title_fontsize'] = None
570+
leg2 = axes[2].legend(title='Aardvark',
571+
title_fontproperties={'family': 'serif'})
572+
assert leg2.get_title().get_fontsize() == mpl.rcParams['font.size']
573+
axes[3].plot(range(10))
574+
leg3 = axes[3].legend(title='Aardvark')
575+
assert leg3.get_title().get_fontsize() == mpl.rcParams['font.size']
576+
axes[4].plot(range(10))
577+
mpl.rcParams['legend.title_fontsize'] = 20
578+
leg4 = axes[4].legend(title='Aardvark',
579+
title_fontproperties={'family': 'serif'})
580+
assert leg4.get_title().get_fontsize() == 20
581+
axes[5].plot(range(10))
582+
leg5 = axes[5].legend(title='Aardvark')
583+
assert leg5.get_title().get_fontsize() == 20
553584

554585

555586
def test_legend_labelcolor_single():

0 commit comments

Comments
 (0)
0