Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 655cffa

Browse files
committed
Update lib/matplotlib/legend.py to simplify the logic as per suggested by jklymak
Remove title_prop from public API and update test for robustness Change title_prop to title_fontproperties
1 parent 4b246ee commit 655cffa

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Legend now has a title_fontproperties kwarg
2+
-------------------------------------------
3+
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:
10+
11+
.. plot::
12+
13+
fig, ax = plt.subplots(figsize=(4, 3))
14+
ax.plot(range(10))
15+
ax.legend(title='Points', title_fontproperties={'family': 'serif'}, title_fontsize=22)

doc/users/next_whats_new/legend_title_prop_kwarg.rst

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/matplotlib/legend.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ 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_prop : None or `matplotlib.font_manager.FontProperties` or dict
240+
title_fontproperties : None or `matplotlib.font_manager.FontProperties` or dict
241241
The font properties of the legend's title. If None (default), the current
242242
:data:`matplotlib.rcParams` will be used.
243243
@@ -327,7 +327,6 @@ def __init__(self, parent, handles, labels,
327327
# box, none use rc
328328
shadow=None,
329329
title=None, # set a title for the legend
330-
title_prop=None, # properties for the legend title
331330
title_fontsize=None, # the font size for the title
332331
framealpha=None, # set frame alpha
333332
edgecolor=None, # frame patch edgecolor
@@ -337,6 +336,7 @@ def __init__(self, parent, handles, labels,
337336
bbox_transform=None, # transform for the bbox
338337
frameon=None, # draw frame
339338
handler_map=None,
339+
title_fontproperties=None, # properties for the legend title
340340
):
341341
"""
342342
Parameters
@@ -511,23 +511,18 @@ def __init__(self, parent, handles, labels,
511511
self._set_loc(loc)
512512
self._loc_used_default = tmp # ignore changes done by _set_loc
513513

514-
# figure out title fontsize:
515-
if title_prop is None:
514+
# figure out title font properties:
515+
title_prop_fp = FontProperties._from_any(title_fontproperties)
516+
if isinstance(title_fontproperties, dict) and \
517+
"size" not in title_fontproperties:
516518
if title_fontsize is None:
517519
title_fontsize = mpl.rcParams["legend.title_fontsize"]
518-
self.title_prop = FontProperties(
519-
size=title_fontsize)
520+
title_prop_fp.set_size(title_fontsize)
520521
else:
521-
self.title_prop = FontProperties._from_any(title_prop)
522-
if isinstance(title_prop, dict) and "size" not in title_prop:
523-
if title_fontsize is None:
524-
title_fontsize = mpl.rcParams["legend.title_fontsize"]
525-
self.title_prop.set_size(title_fontsize)
526-
else:
527-
if title_fontsize is not None:
528-
self.title_prop.set_size(title_fontsize)
522+
if title_fontsize is not None:
523+
title_prop_fp.set_size(title_fontsize)
529524

530-
self.set_title(title, prop=self.title_prop)
525+
self.set_title(title, prop=title_prop_fp)
531526
self._draggable = None
532527

533528
# set the text color

lib/matplotlib/tests/test_legend.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,27 +544,37 @@ def test_window_extent_cached_renderer():
544544
leg2.get_window_extent()
545545

546546

547-
def test_legend_title_fontsize():
547+
def test_legend_title_fontprop_fontsize():
548548
# test the title_fontsize kwarg
549-
fig, axes = plt.subplots(2, 2, figsize=(8, 6))
549+
fig, axes = plt.subplots(2, 3, figsize=(10, 6))
550550
axes = axes.flat
551551
axes[0].plot(range(10))
552552
leg = axes[0].legend(title='Aardvark', title_fontsize=22)
553553
assert leg.get_title().get_fontsize() == 22
554554
axes[1].plot(range(10))
555555
leg2 = axes[1].legend(title='Aardvark',
556-
title_prop={'family': 'serif', 'size': 22})
556+
title_fontproperties={'family': 'serif', 'size': 22})
557557
assert leg2.get_title().get_fontsize() == 22
558558
axes[2].plot(range(10))
559559
leg3 = axes[2].legend(title='Aardvark',
560-
title_prop={'family': 'serif'},
560+
title_fontproperties={'family': 'serif'},
561561
title_fontsize=22)
562562
assert leg3.get_title().get_fontsize() == 22
563563
axes[3].plot(range(10))
564564
leg4 = axes[3].legend(title='Aardvark',
565-
title_prop={'family': 'serif', 'size': 10},
565+
title_fontproperties={'family': 'serif', 'size': 10},
566566
title_fontsize=22)
567567
assert leg4.get_title().get_fontsize() == 22
568+
mpl.rcParams['legend.title_fontsize'] = None
569+
axes[4].plot(range(10))
570+
leg5 = axes[4].legend(title='Aardvark',
571+
title_fontproperties={'family': 'serif'})
572+
assert leg5.get_title().get_fontsize() == mpl.rcParams['font.size']
573+
mpl.rcParams['legend.title_fontsize'] = 20
574+
axes[5].plot(range(10))
575+
leg6 = axes[5].legend(title='Aardvark',
576+
title_fontproperties={'family': 'serif'})
577+
assert leg6.get_title().get_fontsize() == 20
568578

569579

570580
def test_legend_labelcolor_single():

0 commit comments

Comments
 (0)