Skip to content

Add legend title size rc parameter #10121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/users/next_whats_new/2017-12-27_legend_title_size_rc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Legend Title Size rc parameter
------------------------------

A new rc parameter has been added as an option in matplotlibrc allowing you to explicitly control the font size of the legend title.
The default option is ``inherit`` which reverts to the previous behavior of inheriting font size from ``font.size``.

``legend.titlesize = 'large'``

.. code-block:: python

import matplotlib.rcParams as rcParams

rcParams['legend.titlesize'] = 'large'
5 changes: 4 additions & 1 deletion lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,10 @@ def __init__(self, parent, handles, labels,
self.get_frame().set_alpha(framealpha)

self._loc = loc
self.set_title(title)
if rcParams["legend.titlesize"] == 'inherit':
self.set_title(title)
else:
self.set_title(title, prop={"size": rcParams["legend.titlesize"]})
self._last_fontsize_points = self._fontsize
self._draggable = None

Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ def validate_fontsize(s):
validate_fontsizelist = _listify_validator(validate_fontsize)


def validate_fontsize_or_inherit(s):
'return a valid fontsize arg'
if s == 'inherit':
return s
return validate_fontsize(s)


def validate_font_properties(s):
parse_fontconfig_pattern(s)
return s
Expand Down Expand Up @@ -1178,6 +1185,7 @@ def _validate_linestyle(ls):
# the number of points in the legend line for scatter
'legend.scatterpoints': [1, validate_int],
'legend.fontsize': ['medium', validate_fontsize],
'legend.titlesize': ['inherit', validate_fontsize_or_inherit],
# the relative size of legend markers vs. original
'legend.markerscale': [1.0, validate_float],
'legend.shadow': [False, validate_bool],
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ def test_rc():
title="My legend")


def test_rc_title_size():
test_legend_title_size = 18.0
mpl.rcParams['legend.titlesize'] = test_legend_title_size
plt.figure()
ax = plt.subplot(121)
ax.scatter(np.arange(10), np.arange(10, 0, -1), label='two')
leg = ax.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
title="My legend")
assert leg.get_title().get_fontsize() == test_legend_title_size


def test_rc_title_size_inherit():
test_legend_title_size = 18.0
mpl.rcParams['legend.titlesize'] = 'inherit'
mpl.rcParams['font.size'] = test_legend_title_size
plt.figure()
ax = plt.subplot(121)
ax.scatter(np.arange(10), np.arange(10, 0, -1), label='one')
leg = ax.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
title="My Inherited Legend Title")
assert leg.get_title().get_fontsize() == test_legend_title_size


@image_comparison(baseline_images=['legend_expand'], remove_text=True)
def test_legend_expand():
'Test expand mode'
Expand Down
1 change: 1 addition & 0 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ backend : $TEMPLATE_BACKEND
#legend.scatterpoints : 1 # number of scatter points
#legend.markerscale : 1.0 # the relative size of legend markers vs. original
#legend.fontsize : medium
#legend.titlesize : inherit # fontsize of the legend title, or inherit from font.size
# Dimensions as fraction of fontsize:
#legend.borderpad : 0.4 # border whitespace
#legend.labelspacing : 0.5 # the vertical space between the legend entries
Expand Down