8000 make title_fontsize and title_fontproperties incompatible · matplotlib/matplotlib@f5d44e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5d44e0

Browse files
committed
make title_fontsize and title_fontproperties incompatible
1 parent 655cffa commit f5d44e0

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
Legend now has a title_fontproperties kwarg
2-
-------------------------------------------
1+
Font properties of legend title are configurable
2+
------------------------------------------------
33

4-
The title for a `.Figure.legend` and `.Axes.legend` can now have its
5-
title's font properties set via the ``title_fontproperties`` kwarg, defaults
6-
to ``None``, which means the legend's title will have the font properties
7-
as set by the :rc:`legend.title_fontsize`. Through this new kwarg, one
8-
can set the legend's title font properties through either FontProperties
9-
or dict, for example:
4+
Title's font properties can be set via the *title_fontproperties* keyword
5+
argument, for example:
106

117
.. plot::
128

139
fig, ax = plt.subplots(figsize=(4, 3))
14-
ax.plot(range(10))
15-
ax.legend(title='Points', title_fontproperties={'family': 'serif'}, title_fontsize=22)
10+
ax.plot(range(10),label='point')
11+
ax.legend(title='Points', title_fontproperties={'family': 'serif', 'size': 20})

lib/matplotlib/legend.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,16 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
238238
The legend's title. Default is no title (``None``).
239239
240240
title_fontproperties : None or `matplotlib.font_manager.FontProperties` or dict
241-
The font properties of the legend's title. If None (default), the current
242-
:data:`matplotlib.rcParams` will be used.
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.
243244
244245
title_fontsize : int or {'xx-small', 'x-small', 'small', 'medium', 'large', \
245246
'x-large', 'xx-large'}, default: :rc:`legend.title_fontsize`
246247
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*.
247251
248252
borderpad : float, default: :rc:`legend.borderpad`
249253
The fractional whitespace inside the legend border, in font-size units.
@@ -512,15 +516,20 @@ def __init__(self, parent, handles, labels,
512516
self._loc_used_default = tmp # ignore changes done by _set_loc
513517

514518
# 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. ")
515523
title_prop_fp = FontProperties._from_any(title_fontproperties)
516-
if isinstance(title_fontproperties, dict) and \
517-
"size" not in title_fontproperties:
518-
if title_fontsize is None:
524+
if isinstance(title_fontproperties, dict):
525+
if "size" not in title_fontproperties:
519526
title_fontsize = mpl.rcParams["legend.title_fontsize"]
520-
title_prop_fp.set_size(title_fontsize)
521-
else:
522-
if title_fontsize is not None:
523527
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)
524533

525534
self.set_title(title, prop=title_prop_fp)
526535
self._draggable = None

lib/matplotlib/tests/test_legend.py

Lines changed: 25 additions & 19 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():
@@ -546,35 +547,40 @@ def test_window_extent_cached_renderer():
546547

547548
def test_legend_title_fontprop_fontsize():
548549
# test the title_fontsize kwarg
550+
plt.plot(range(10))
551+
with pytest.raises(ValueError):
552+
plt.legend(title='Aardvark', title_fontsize=22,
553+
title_fontproperties={'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+
549559
fig, axes = plt.subplots(2, 3, figsize=(10, 6))
550560
axes = axes.flat
551561
axes[0].plot(range(10))
552-
leg = axes[0].legend(title='Aardvark', title_fontsize=22)
553-
assert leg.get_title().get_fontsize() == 22
562+
leg0 = axes[0].legend(title='Aardvark', title_fontsize=22)
563+
assert leg0.get_title().get_fontsize() == 22
554564
axes[1].plot(range(10))
555-
leg2 = axes[1].legend(title='Aardvark',
565+
leg1 = axes[1].legend(title='Aardvark',
556566
title_fontproperties={'family': 'serif', 'size': 22})
557-
assert leg2.get_title().get_fontsize() == 22
567+
assert leg1.get_title().get_fontsize() == 22
558568
axes[2].plot(range(10))
559-
leg3 = axes[2].legend(title='Aardvark',
560-
title_fontproperties={'family': 'serif'},
561-
title_fontsize=22)
562-
assert leg3.get_title().get_fontsize() == 22
563-
axes[3].plot(range(10))
564-
leg4 = axes[3].legend(title='Aardvark',
565-
title_fontproperties={'family': 'serif', 'size': 10},
566-
title_fontsize=22)
567-
assert leg4.get_title().get_fontsize() == 22
568569
mpl.rcParams['legend.title_fontsize'] = None
569-
axes[4].plot(range(10))
570-
leg5 = axes[4].legend(title='Aardvark',
570+
leg2 = axes[2].legend(title='Aardvark',
571571
title_fontproperties={'family': 'serif'})
572-
assert leg5.get_title().get_fontsize() == mpl.rcParams['font.size']
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))
573577
mpl.rcParams['legend.title_fontsize'] = 20
574-
axes[5].plot(range(10))
575-
leg6 = axes[5].legend(title='Aardvark',
578+
leg4 = axes[4].legend(title='Aardvark',
576579
title_fontproperties={'family': 'serif'})
577-
assert leg6.get_title().get_fontsize() == 20
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
578584

579585

580586
def test_legend_labelcolor_single():

0 commit comments

Comments
 (0)
0