-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
There was a problem hiding this 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 | |||
--------------------- |
There was a problem hiding this comment.
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
matplotlibrc.template
Outdated
@@ -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 |
There was a problem hiding this comment.
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?
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 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. |
I have addressed the following changes:
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). |
There was a problem hiding this 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...
matplotlibrc.template
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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. |
There was a problem hiding this comment.
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'` |
There was a problem hiding this comment.
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'
The latest commit addresses your comments @dstansby, thanks for the feedback. The documentation is much clearer now. |
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' |
This latest commit changes the default behavior to inherit as suggested by @tacaswell. For this change I have:
|
We need 2 approvals, and no disapprovals, so you are at the mercy of @dstansby! |
Nice one, sorry I didn't get back into this |
Ping @dstansby, Also I see there's now a merge conflict on |
You should rebase this branch onto the current master.
If neccessary, resolve any conflicts and force-push your feature-branch again. When all looks good you can delete your backup branch: |
I think the functionality is much more easily provided via #11172, which would just need to be merged. |
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