-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fixed a bug where rcParams settings were being ignored for formatting axes labels #25237
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.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a while, please feel free to ping @matplotlib/developers
or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join us on gitter for real-time discussion.
For details on testing, writing docs, and our review process, please see the developer guide
We strive to be a welcoming and open project. Please follow our Code of Conduct.
@@ -879,6 +879,9 @@ def clear(self): | |||
- registered callbacks | |||
""" | |||
self.label._reset_visual_defaults() | |||
self.label.set_color(mpl.rcParams['axes.labelcolor']) |
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.
Shouldn't this all happen in _reset_visual_defaults
?
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.
self.label
is simply a Text
instance, so self.label._reset_visual_defaults()
looks, for example, at the text.color
rcParams instead of the axes.labelcolor
rcParams.
self.label._reset_visual_defaults()
does accept input arguments to override text defaults (although arguably that that doesn't make complete sense given the name of the method). So, for color in specific, one could call self.label._reset_visual_defaults(color=mpl.rcParams['axes.labelcolor'])
. For axes.labelsize
and axes.labelweight
, it takes a little more effort because those have to be passed in through the fontproperties
keyword argument.
Ultimately, I decided that it was overly complicated to pass in those arguments to _reset_visual_defaults()
or to modify that method for self.label
to be aware of the defaults for an axis label rather than the defaults for text. The approach that I took in this PR felt more straightforward.
I have added a clarifying comment for future maintainers.
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 good, but I think you need to wrap the test in a style context, rather than set the parameter values as those will persist. However, I could be mistaken in how the decorator works....
|
||
@mpl.style.context('default') | ||
def test_rc_axes_label_formatting(): | ||
mpl.rcParams['axes.labelcolor'] = 'red' |
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.
mpl.rcParams['axes.labelcolor'] = 'red' | |
mpl.rcParams['axes.labelcolor'] = 'red' |
I think these should be wrapped in an rcContext, otherwise they apply for the rest of the test file, which could be confusing for future 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 believe it should be fine because the matplotlib.style.context
decorator already wraps everything in an rc_context
:
matplotlib/lib/matplotlib/style/core.py
Line 220 in f726d2c
with mpl.rc_context(): |
Admittedly, I have not tested it.
Thanks @ayshih I squashed your commits. Congratulations on your first PR to Matplotlib! |
…e being ignored for formatting axes labels
Great, thanks! |
…237-on-v3.7.x Backport PR #25237 on branch v3.7.x (Fixed a bug where rcParams settings were being ignored for formatting axes labels)
PR Summary
Matplotlib 3.7.0 has a bug where rcParams settings are being ignored for formatting axes labels. This bug was introduced with #21253, specifically the use of the
Text._reset_visual_defaults()
method to reset the formatting of an axis label, including during the initialization of an axis. Since the axis label is just aText
, the color gets reset totext.color
instead ofaxes.labelcolor
, andaxes.labelsize
andaxes.labelweight
are ignored. (This bug is not readily apparent whentext.color
andaxes.labelcolor
happen to be the same, as is common for many styles.)Example script:
Output on Matplotlib 3.7.0:

Output with this PR:

PR Checklist
Documentation and Tests
pytest
passes)Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/ 10000 next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst