Skip to content

Don't set savefig.facecolor/edgecolor in dark_background/538 styles. #28156

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 1 commit into from
May 2, 2024

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 30, 2024

Previously, the dark_background and fivethirtyeight styles would set the savefig.facecolor and savefig.edgecolor rcParams to the same values as figure.facecolor and figure.edgecolor; i.e., if a user uses a style but tweaks the figure facecolor, this tweak is lost (overridden) when saving the figure. A concrete example would be using the dark_background style but wanting a dark gray background instead of a black one, e.g. mpl.style.use(["dark_background", {"figure.facecolor": ".2"}]); plot(); savefig(...).

In all likelihood these values were set because the styles predate the ability to set these savefig rcParams to "auto", which means "don't change the figure facecolor, just use it as is" (a feature introduced in #15111 exactly because this auto-switching of color by savefig was confusing users). Now we can just remove these rcParams from the two styles (which are the only ones affected by the issue -- the grayscale style also sets savefig.facecolor but to a value different from figure.facecolor) and let them inherit the global default value, "auto".

PR summary

PR checklist

@tacaswell tacaswell added this to the v3.10.0 milestone Apr 30, 2024
@timhoffm
Copy link
Member

Why do we have savefig.facecolor at all? Wouldn’t it be reasonable to always save the figure as is? If a user wants a different background, they can configure the figure background directly.

@tacaswell
Copy link
Member

This is a hold over from the 1.x days when the figure face color was gray and we had a second face color (which defaulted to 'w' for saving). With the 2.0 default changes we switched the default face color when shown to be white and defaulted the save face color to "auto".

@@ -0,0 +1,12 @@
dark_background and fivethirtyeight styles no longer set ``savefig.facecolor`` and ``savefig.edgecolor``
Copy link
Member

Choose a reason for hiding this comment

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

This filename is not updated with the PR number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, fixed

Previously, the dark_background and fivethirtyeight styles would set the
savefig.facecolor and savefig.edgecolor rcParams to the same values as
figure.facecolor and figure.edgecolor; i.e., if a user uses a style but
tweaks the figure facecolor, this tweak is lost (overridden) when saving
the figure.  A concrete example would be using the dark_background style
but wanting a dark gray background instead of a black one, e.g.
`mpl.style.use(["dark_background", {"figure.facecolor": ".2"}]); plot();
savefig(...)`.

In all likelihood these values were set because the styles
predate the ability to set these savefig rcParams to "auto", which means
"don't change the figure facecolor, just use it as is" (a feature
introduced exactly because this auto-switching of color by savefig was
confusing users).  Now we can just remove these rcParams from the two
styles (which are the only ones affected by the issue -- the grayscale
style also sets savefig.facecolor but to a value different from
figure.facecolor) and let them inherit the global default value, "auto".
@tacaswell tacaswell merged commit 378a77a into matplotlib:main May 2, 2024
43 of 44 checks passed
@tacaswell
Copy link
Member

The appevyor failure looks sphinx related.

@anntzer anntzer deleted the dbsfc branch May 2, 2024 13:39
@wshanks
Copy link

wshanks commented Jan 6, 2025

I don't think there is any bug to report, but I just want to note one thing that confused me for a while when going from 3.9 to 3.10 with this change in case it helps someone else. For code like:

pyplot.gcf()
pyplot.style.use("dark_background")
<generate plot...>
pyplot.savefig("fig.png")

With 3.9, this code will produce a figure with a black border. With 3.10, it will produce a figure with a white border because the gcf() call will create the figure with the default facecolor (assuming this is the first code called and there is no existing figure) and then the style won't override the border color in the savefig command any more. In my case, I changed this code to use the object-oriented style with a new figure rather than using gcf() to avoid inheriting a facecolor.

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.

5 participants