8000 ENH: add title_fontsize to legend by jklymak · Pull Request #10715 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

ENH: add title_fontsize to legend #10715

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

Merged
merged 2 commits into from
May 4, 2018
Merged
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
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/legend_title_fontsize_kwarg.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Legend now has a title_fontsize kwarg
-------------------------------------

The title for a `.Figure.legend` and `.Axes.legend` can now have its
fontsize set via the ``title_fontsize`` kwarg, defaults to ``None``, which
means the legend title will have the same fontsize as the axes default
fontsize (*not* the legend fontsize, set by the ``fontsize`` kwarg or
:rc:`legend.fontsize`).
12 changes: 10 additions & 2 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
title : str or None
The legend's title. Default is no title (``None``).

title_fontsize: str or None
The fontsize of the legend's title. Default is the default fontsize.

borderpad : float or None
The fractional whitespace inside the legend border.
Measured in font-size units.
Expand Down Expand Up @@ -333,7 +336,7 @@ def __init__(self, parent, handles, labels,
# box, none use rc
shadow=None,
title=None, # set a title for the legend

title_fontsize=None, # set to ax.fontsize if None
framealpha=None, # set frame alpha
edgecolor=None, # frame patch edgecolor
facecolor=None, # frame patch facecolor
Expand Down Expand Up @@ -539,7 +542,12 @@ def __init__(self, parent, handles, labels,
self.get_frame().set_alpha(framealpha)

self._loc = loc
self.set_title(title)
# figure out title fontsize:
if title_fontsize is not None:
tprop = FontProperties(size=title_fontsize)
else:
tprop = None
self.set_title(title, prop=tprop)
self._last_fontsize_points = self._fontsize
self._draggable = None

Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,11 @@ def test_legend_proper_window_extent():
leg = ax.legend()
x02 = leg.get_window_extent(fig.canvas.get_renderer()).x0
assert pytest.approx(x01*2, 0.1) == x02


def test_legend_title_fontsize():
# test the title_fontsize kwarg
fig, ax = plt.subplots()
ax.plot(range(10))
leg = ax.legend(title='Aardvark', title_fontsize=22)
assert leg.get_title().get_fontsize() == 22
0