Skip to content

DOC: better document rcParams in savefig.* grouping #9030

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
HDembinski opened this issue Aug 14, 2017 · 18 comments · Fixed by #9038
Closed

DOC: better document rcParams in savefig.* grouping #9030

HDembinski opened this issue Aug 14, 2017 · 18 comments · Fixed by #9038
Milestone

Comments

@HDembinski
Copy link

Feature request

Feature summary

The FAQ https://matplotlib.org/faq/howto_faq.html describes a way to make the figure patch transparent, see "Save transparent figures". It would be great to have an option for the matplotlibrc file to always make the figure patch transparent.

Code

As described in the FAQ, the following code makes the figure patch transparent.

from matplotlib import pyplot as plt
fig = plt.figure()
fig.patch.set_alpha(0.0) # set this via rcParams?

I am aware of the rcParams option "savefig.transparent" which makes the figure patch and the axis patch transparent. I only want the figure patch to be transparent, because this looks very nice on slides with a gradient on the background.

Matplotlib version

  • Operating System: MacOS
  • Matplotlib Version: 2.0.2
  • Python Version: 2.7.10
@HDembinski
Copy link
Author

Related issue: #2384

@HDembinski
Copy link
Author

I am happy to work on a PR if this has a chance of being accepted.

@tacaswell
Copy link
Member

The facecolor can be an RGBA value so setting

mpl.rcParams['figure.facecolor'] = (0, 0, 0, 0)

should do what you want?

@HDembinski
Copy link
Author

Hi Thomas,

I tried this, but full RGBA values are not supported in matplotlibrc. When I set this

figure.facecolor  : (0, 0, 0, 0)

I get this error:

/Users/hdembins/Library/Python/2.7/lib/python/site-packages/matplotlib/__init__.py:1120: UserWarning: Bad val "(0, 0, 0, 0)" on line #26
    "figure.facecolor  : (0, 0, 0, 0)
"
    in file "/Users/hdembins/.matplotlib/matplotlibrc"
    Key figure.facecolor: (0, 0, 0, 0) does not look like a color arg
Color tuples must be length 3
  (val, error_details, msg))

@HDembinski
Copy link
Author

So perhaps the more general solution is to allow full RGBA colors in matplotlibrc.

@HDembinski
Copy link
Author

Update: It also turns out the following code does not work as expected:

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams["figure.facecolor"] = (0, 0, 0, 0)

plt.figure()
plt.plot([], [])
plt.savefig("testme.png")

Opening testme.png shows a white background, not a transparent background for the figure patch. I think that is a bug. The following works:

import matplotlib.pyplot as plt

fig = plt.figure()
fig.patch.set_alpha(0)
plt.plot([], [])
plt.savefig("testme2.png")

See the result:
screen shot 2017-08-15 at 10 50 15

@jenshnielsen
Copy link
Member

To me it looks like the figure is transparent but the axis is not. That is what I would expect when you set the figure transparent and the axes facecolor has its default value which is white.
I would expect

rcParams["axes.facecolor"] = (0, 0, 0, 0)

to do the trick

@HDembinski
Copy link
Author

Hi Jens, yes, I had the same expectation from my naive point of view.

And just to clarify: the attached picture (testme2.png) is the desired outcome that I want.

@HDembinski
Copy link
Author

Hi Jens, sorry, I was not reading closely enough what you wrote.

rcParams["axes.facecolor"] = (0, 0, 0, 0)

does not work either, it produces just a white figure background and a white axes background. Also, I don't want the axes background to be transparent, only the figure background.

@jenshnielsen
Copy link
Member

Ok I assumed that the attached image was the broken behaviour

@anntzer
Copy link
Contributor

anntzer commented Aug 15, 2017

The right rcparam to set is savefig.facecolor.
Right now you can work around the lack of support for 4-tuples using #00000000 (i.e. #rrggbbaa format, introduced in #6382); #9038 adds support for 4-tuples.

@HDembinski
Copy link
Author

HDembinski commented Aug 15, 2017

@anntzer Thanks for the related PR :). I take whatever works, but I think that setting savefig.facecolor to get the desired result is not intuitive.

@tacaswell
Copy link
Member

Hmm, we change savefig.dpi to default to 'figure', I thought we had done the same with the rest of them.

There is also 'savefig.transparent' which I think does the same thing in a more direct way (which I had forgotten about apparently).

@tacaswell tacaswell added this to the 2.2 (next next feature release) milestone Aug 15, 2017
@tacaswell tacaswell changed the title Feature request: rcParams option to permanently make figure background transparent but not axis background DOC: better document rcParams in savefig.* grouping Aug 15, 2017
@HDembinski
Copy link
Author

@tacaswell I read about savefig.transparent but it does not what I need. It makes the figure background and the axis background transparent. I only want the figure background transparent, but keep the axes background white, i.e. what is shown in the screenshot above.

@HDembinski
Copy link
Author

@anntzer I can confirm that this code

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams["savefig.facecolor"] = (0, 0, 0, 0)

fig = plt.figure()
plt.plot([], [])
plt.savefig("testme2.png")

produces exactly what I want. Thanks! 😄

Following your advice as I understood it, I tried to add this line to my matplotlibrc:

savefig.facecolor: #00000000

But I then get this error:

/Users/hdembins/Library/Python/2.7/lib/python/site-packages/matplotlib/__init__.py:1120: UserWarning: Bad val """ on line #31
    "savefig.facecolor: "#00000000""
    in file "/Users/hdembins/.matplotlib/matplotlibrc"
    Key savefig.facecolor: " does not look like a color arg
  (val, error_details, msg))

@HDembinski
Copy link
Author

Okay, looking into the relevant code myself helps ;)
It works with the current matplotlib 2.0.2, if I put this line into matplotlibrc

savefig.facecolor: 00000000

Note that one has to omit the #-prefix, which is not intuitive. I think the color format in matplotlibrc should be identical to the one in Python.

@HDembinski
Copy link
Author

Ok, nevermind, the # in matplotlibrc starts a comment, that's why the color cannot be prefixed like this. I guess it is not worth the effort to make an exception in the parser for this sort of thing.

@QuLogic QuLogic modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Aug 22, 2017
@HDembinski
Copy link
Author

This is fantastic. Thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants