8000 Fixed a bug where rcParams settings were being ignored for formatting axes labels by ayshih · Pull Request #25237 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 2 commits into from
Feb 21, 2023
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
5 changes: 5 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,11 @@ def clear(self):
- registered callbacks
"""
self.label._reset_visual_defaults()
# The above resets the label formatting using text rcParams,
# so we then update the formatting using axes rcParams
self.label.set_color(mpl.rcParams['axes.labelcolor'])
Copy link
Member

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?

Copy link
Contributor Author

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.

self.label.set_fontsize(mpl.rcParams['axes.labelsize'])
self.label.set_fontweight(mpl.rcParams['axes.labelweight'])
self.offsetText._reset_visual_defaults()
self.labelpad = mpl.rcParams['axes.labelpad']

Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8431,3 +8431,15 @@ def test_zorder_and_explicit_rasterization():
ln, = ax.plot(range(5), rasterized=True, zorder=1)
with io.BytesIO() as b:
fig.savefig(b, format='pdf')


@mpl.style.context('default')
def test_rc_axes_label_formatting():
mpl.rcParams['axes.labelcolor'] = 'red'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

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 believe it should be fine because the matplotlib.style.context decorator already wraps everything in an rc_context:

with mpl.rc_context():

Admittedly, I have not tested it.

mpl.rcParams['axes.labelsize'] = 20
mpl.rcParams['axes.labelweight'] = 'bold'

ax = plt.axes()
assert ax.xaxis.label.get_color() == 'red'
assert ax.xaxis.label.get_fontsize() == 20
assert ax.xaxis.label.get_fontweight() == 'bold'
0