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

Skip to content

Commit 4b246ee

Browse files
committed
Add title_prop keyword to matplotlib.pyplot.legend
1 parent 92ce41d commit 4b246ee

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
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_prop 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_prop`` kwarg, defaults to
6+
``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_prop={'family': 'serif'}, title_fontsize=22)

lib/matplotlib/legend.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ 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
241+
The font properties of the legend's title. If None (default), the current
242+
:data:`matplotlib.rcParams` will be used.
243+
240244
title_fontsize : int or {'xx-small', 'x-small', 'small', 'medium', 'large', \
241245
'x-large', 'xx-large'}, default: :rc:`legend.title_fontsize`
242246
The font size of the legend's title.
@@ -323,6 +327,7 @@ def __init__(self, parent, handles, labels,
323327
# box, none use rc
324328
shadow=None,
325329
title=None, # set a title for the legend
330+
title_prop=None, # properties for the legend title
326331
title_fontsize=None, # the font size for the title
327332
framealpha=None, # set frame alpha
328333
edgecolor=None, # frame patch edgecolor
@@ -507,10 +512,22 @@ def __init__(self, parent, handles, labels,
507512
self._loc_used_default = tmp # ignore changes done by _set_loc
508513

509514
# 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)
515+
if title_prop is None:
516+
if title_fontsize is None:
517+
title_fontsize = mpl.rcParams["legend.title_fontsize"]
518+
self.title_prop = FontProperties(
519+
size=title_fontsize)
520+
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)
529+
530+
self.set_title(title, prop=self.title_prop)
514531
self._draggable = None
515532

516533
# set the text color

lib/matplotlib/tests/test_legend.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,25 @@ def test_window_extent_cached_renderer():
546546

547547
def test_legend_title_fontsize():
548548
# test the title_fontsize kwarg
549-
fig, ax = plt.subplots()
550-
ax.plot(range(10))
551-
leg = ax.legend(title='Aardvark', title_fontsize=22)
549+
fig, axes = plt.subplots(2, 2, figsize=(8, 6))
550+
axes = axes.flat
551+
axes[0].plot(range(10))
552+
leg = axes[0].legend(title='Aardvark', title_fontsize=22)
552553
assert leg.get_title().get_fontsize() == 22
554+
axes[1].plot(range(10))
555+
leg2 = axes[1].legend(title='Aardvark',
556+
title_prop={'family': 'serif', 'size': 22})
557+
assert leg2.get_title().get_fontsize() == 22
558+
axes[2].plot(range(10))
559+
leg3 = axes[2].legend(title='Aardvark',
560+
title_prop={'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_prop={'family': 'serif', 'size': 10},
566+
title_fontsize=22)
567+
assert leg4.get_title().get_fontsize() == 22
553568

554569

555570
def test_legend_labelcolor_single():

0 commit comments

Comments
 (0)