From 156131cc29ef15f7a09e9e2f8d3f38994104a5a5 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 4 May 2018 11:04:21 -0700 Subject: [PATCH 1/2] ENH: add title_fontsize to legend --- doc/users/next_whats_new/legend_title_fontsize_kwarg.rst | 9 +++++++++ lib/matplotlib/legend.py | 8 ++++++-- lib/matplotlib/rcsetup.py | 8 ++++++++ lib/matplotlib/tests/test_legend.py | 8 ++++++++ matplotlibrc.template | 1 + 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 doc/users/next_whats_new/legend_title_fontsize_kwarg.rst diff --git a/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst b/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst new file mode 100644 index 000000000000..abaab153eca0 --- /dev/null +++ b/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst @@ -0,0 +1,9 @@ +Legend now has a title_fontsize kwarg (and rcParam) +--------------------------------------------------- + +The title for a `.Figure.legend` and `.Axes.legend` can now have its +fontsize set via the ``title_fontsize`` kwarg. There is also a new +:rc:`legend.title_fontsize`. Both default 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`). diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 585915e0c554..866ee6d7aa12 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -333,7 +333,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 @@ -539,7 +539,11 @@ 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 None: + title_fontsize = rcParams['legend.title_fontsize'] + tprop = FontProperties(size=title_fontsize) + self.set_title(title, prop=tprop) self._last_fontsize_points = self._fontsize self._draggable = None diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 7f2e9806310d..99c643edcf6c 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -420,6 +420,13 @@ def validate_aspect(s): raise ValueError('not a valid aspect specification') +def validate_fontsize_None(s): + if s is None or s == 'None': + return None + else: + return validate_fontsize(s) + + def validate_fontsize(s): fontsizes = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'smaller', 'larger'] @@ -1226,6 +1233,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.title_fontsize': [None, validate_fontsize_None], # the relative size of legend markers vs. original 'legend.markerscale': [1.0, validate_float], 'legend.shadow': [False, validate_bool], diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index b1d176ec8a0f..6e377505a078 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -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 diff --git a/matplotlibrc.template b/matplotlibrc.template index f4549755ed01..6d8654d05eeb 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -415,6 +415,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.title_fontsize : None ## None sets to the same as the default axes. ## Dimensions as fraction of fontsize: #legend.borderpad : 0.4 ## border whitespace #legend.labelspacing : 0.5 ## the vertical space between the legend entries From fcdf8c308fee4544706d0ff06daf81dee2b5d8b8 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 4 May 2018 13:25:20 -0700 Subject: [PATCH 2/2] ENH/MNT: drop the rcparam from legend.title_fontsize --- .../next_whats_new/legend_title_fontsize_kwarg.rst | 13 ++++++------- lib/matplotlib/legend.py | 10 +++++++--- lib/matplotlib/rcsetup.py | 8 -------- matplotlibrc.template | 1 - 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst b/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst index abaab153eca0..6544e42afb9a 100644 --- a/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst +++ b/doc/users/next_whats_new/legend_title_fontsize_kwarg.rst @@ -1,9 +1,8 @@ -Legend now has a title_fontsize kwarg (and rcParam) ---------------------------------------------------- +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. There is also a new -:rc:`legend.title_fontsize`. Both default 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`). +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`). diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 866ee6d7aa12..08b01f82b40d 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -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. @@ -540,9 +543,10 @@ def __init__(self, parent, handles, labels, self._loc = loc # figure out title fontsize: - if title_fontsize is None: - title_fontsize = rcParams['legend.title_fontsize'] - tprop = FontProperties(size=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 diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 99c643edcf6c..7f2e9806310d 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -420,13 +420,6 @@ def validate_aspect(s): raise ValueError('not a valid aspect specification') -def validate_fontsize_None(s): - if s is None or s == 'None': - return None - else: - return validate_fontsize(s) - - def validate_fontsize(s): fontsizes = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'smaller', 'larger'] @@ -1233,7 +1226,6 @@ 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.title_fontsize': [None, validate_fontsize_None], # the relative size of legend markers vs. original 'legend.markerscale': [1.0, validate_float], 'legend.shadow': [False, validate_bool], diff --git a/matplotlibrc.template b/matplotlibrc.template index 6d8654d05eeb..f4549755ed01 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -415,7 +415,6 @@ 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.title_fontsize : None ## None sets to the same as the default axes. ## Dimensions as fraction of fontsize: #legend.borderpad : 0.4 ## border whitespace #legend.labelspacing : 0.5 ## the vertical space between the legend entries