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

Conversation

Raab70
Copy link
Contributor

@Raab70 Raab70 commented Dec 27, 2017

PR Summary

Fixes #9201 by adding an rc parameter to customize legend title size. Following the checklist from that issue to add this parameter. This allows for customization of the legend title font size from the matplotlibrc file, a requested feature addition.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small changes, but 👍 overall

Legend Title Size rc parameter
---------------------

A new rc parameter has been added as an option in your matplotlibrc allowing you to control the font size of the legend title.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is longer than 79 characters, so is failing the PEP8 tests - please could you make it <= 79 characters on each line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be pep8 checking the rst files....

If we are that seems like a bug in the tests.

Copy link
Member

@QuLogic QuLogic Dec 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @dstansby misread something; pep8 is failing in test_legend.py:

.../lib/matplotlib/tests/test_legend.py:193:1: E302 expected 2 blank lines, found 1

@@ -0,0 +1,6 @@
Legend Title Size rc parameter
---------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the underline needs to be exactly the same length as the title

@@ -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 : medium # size of the legend title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be "fontsize of the legend title" just to be clear?

@tacaswell tacaswell added this to the v2.2 milestone Dec 29, 2017
@tacaswell
Copy link
Member

tacaswell commented Dec 29, 2017

Thanks for your work on this @Raab70 !

Just a minor formatting change in the formatting of the tests and an rst issue and this looks good to go. It is a bit odd to have something that is only controlled by an rcparam (rather than a function argument) but there is currently no control over this (other than just reaching in and poking at the Text object) and it is definitely something that makes sense to control via a stylesheet. If and when someone asks for per-call-to-legend control of this we will sort out how to do kwargs with this.

The white-space-in-the-tests issue may seem petty / annoying, but having automated tests on codestyle really does help on a codebase the size of Matplotlib.

@Raab70
Copy link
Contributor Author

Raab70 commented Dec 29, 2017

I have addressed the following changes:

  • Fixed the title designation on the next_whats_next doc
  • Fixed the pep8 error on the test
  • Clarified the matplotlibrc text as requested

Thanks for all your help. I don't think any of this is petty, definitely impressive the amount of time you have all put into such a small PR. I appreciate it. This is my first issue and I look forward to more (suggestions welcome! This was way easy and I look forward to tackling some harder ones).

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree w/ @tacaswell that its a bit odd to not also have a kwarg for this...

@@ -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 : medium # fontsize of the legend title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change? Because the legend fontsize used to be the axes fontsize, so if someone changed the axes fontsize they may expect the legend title to also change size. So... I think it needs an API entry as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're saying. On unpatched matplotlib:

In [28]: mpl.rcParams.update({'font.size': 22})
In [29]: ax = plt.subplot(111);ax.scatter(np.arange(0,5), np.arange(0,5), label='One')
In [30]: ax.legend(fontsize=18, title='My Great Legend')
In [31]: ax.get_legend().get_title().get_fontsize()
Out[31]: 22.0

With this patch that new font size would be this default value. Suggestions for how to proceed? Just an entry in the API doc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two choices, I guess. Either just add an API change. I'd personally be fine w/ that.

Or, if you want to be fancy, the default could be "None", in which case it could be made to just use the axes fontsize. If the user specifies a fontsize, then that gets used instead.

Given that there was no test that broke, I assume the default is 'medium'. OTOH, its a bit of a pain to set all the different fontsizes if you want them to be bigger, and this one is particularly obscure. So maybe "None" is the way to go? I don't feel strongly either way.

I'm not sure off the top of my head how "None" gets parsed in the rcParams, but there are a few entries with a None entry, so I imagine you could figure it out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked again and there is no validate_fontsize_or_none validation and I do t see why you should do it here. I’d say just add an API doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a first pass at an API doc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option here is to default to 'inherit' which falls back to not passing anything into set_title (we do this a few other places).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell I could do that. Do we also want this to be a kwarg?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Raab70 if you take a look at rcsetup.py you'll see how validate_color_or_inherit works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine either way about adding a kwarg. On one hand, it would be reasonable to expect an kwarg on the other hand legend already has too many kwrags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I would go with your comment above to keep the clutter down for something that is not likely to be used very often.

If and when someone asks for per-call-to-legend control of this we will sort out how to do kwargs with this.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This looks good to me. Sorry for a couple more fussy things.

I'm not going to approve, just because I think someone more senior should approve this. The change is somewhat of a breaking change, and I don't have the experience to know how much guff from downstream packages this might cause.

New rcParam for Legend Title Font Size
``````````````````````````````````````

The introduction of a new rcParam for legend title font size (``legend.titlesize``) deprecates the previous usage of the ``font.size`` rcParam for modification of the legend title font size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean "deprecate" -> "replaces". To me, at least, "deprecation" means you will still let it work, but it will stop working at some point. You aren't deprecating, you are doing a hard replace.

I think this should explicitly state that old code where font.size was changed will now also need to change legend.titlesize to get the same effect, and that the default is medium (as is font.size).


A new rc parameter has been added as an option in your matplotlibrc allowing you to control the font size of the legend title.

`legend.titlesize = 'large'`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little ambiguous to a new user as to where they would enter this command. I'd be more explicit with a bit of code:

.. code-block:: python
    import matplotlib.rcParams as rcParams
    
    rcParams['legend.titlesize'] = 'large'

@Raab70
Copy link
Contributor Author

Raab70 commented Dec 30, 2017

The latest commit addresses your comments @dstansby, thanks for the feedback. The documentation is much clearer now.

@jklymak
Copy link
Member

jklymak commented Jan 2, 2018

Ooops, sorry, you need an extra carriage return as per the error linked above (under details). Sorry for the mistype.

.. code-block:: python

    import matplotlib.rcParams as rcParams
    
    rcParams['legend.titlesize'] = 'large'

@Raab70
Copy link
Contributor Author

Raab70 commented Jan 3, 2018

This latest commit changes the default behavior to inherit as suggested by @tacaswell. For this change I have:

  • Added a new validate_fontsize_or_inherit function to rcsetup.py
  • Made the appropriate changes for the inherit option to work in legend.py
  • Updated the next_whats_next documentation to reflect this change
  • Removed the api doc since this is no longer a breaking change
  • Added a test that inherit is working properly

@jklymak
Copy link
Member

jklymak commented Jan 6, 2018

ping @dstansby - I think @Raab70 covered all the suggestions...

@Raab70
Copy link
Contributor Author

Raab70 commented Feb 2, 2018

ping @jklymak @dstansby Is there anything else needed on this?

@jklymak
Copy link
Member

jklymak commented Feb 2, 2018

We need 2 approvals, and no disapprovals, so you are at the mercy of @dstansby!

@andrewfowlie
Copy link

Nice one, sorry I didn't get back into this

@Raab70
Copy link
Contributor Author

Raab70 commented Mar 5, 2018

Ping @dstansby, Also I see there's now a merge conflict on matplotlibrc.template, I guess that means I need to merge upstream changes into this branch? I'm not certain I understand the conflict by clicking the link in here. It's a rather poor interface.

@timhoffm
Copy link
Member

timhoffm commented May 6, 2018

Also I see there's now a merge conflict on matplotlibrc.template, I guess that means I need to merge upstream changes into this branch?

You should rebase this branch onto the current master.

# Update the mirror of trunk
git fetch upstream
# go to the feature branch
git checkout feature/legend-title-rcparam
# make a backup in case you mess up
git branch tmp feature/legend-title-rcparam
# rebase
git rebase upstream/master

If neccessary, resolve any conflicts and force-push your feature-branch again.

When all looks good you can delete your backup branch: git branch -D tmp

@ImportanceOfBeingErnest
Copy link
Member

I think the functionality is much more easily provided via #11172, which would just need to be merged.

@Raab70 Raab70 closed this May 6, 2018
@QuLogic QuLogic removed this from the needs sorting milestone May 6, 2018
@Raab70 Raab70 deleted the feature/legend-title-rcparam branch September 2, 2018 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

matplotlibrc parameter for legend title font size
8 participants