You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DOC: better document rcParams in savefig.* grouping #9030
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.
frommatplotlibimportpyplotaspltfig=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.
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))
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
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.
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.
changed the title [-]Feature request: rcParams option to permanently make figure background transparent but not axis background[/-][+]DOC: better document rcParams in savefig.* grouping[/+]on Aug 15, 2017
@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.
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))
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.
Activity
HDembinski commentedon Aug 14, 2017
Related issue: #2384
HDembinski commentedon Aug 14, 2017
I am happy to work on a PR if this has a chance of being accepted.
tacaswell commentedon Aug 14, 2017
The facecolor can be an RGBA value so setting
should do what you want?
HDembinski commentedon Aug 15, 2017
Hi Thomas,
I tried this, but full RGBA values are not supported in matplotlibrc. When I set this
I get this error:
HDembinski commentedon Aug 15, 2017
So perhaps the more general solution is to allow full RGBA colors in matplotlibrc.
HDembinski commentedon Aug 15, 2017
Update: It also turns out the following code does not work as expected:
Opening testme.png shows a white background, not a transparent background for the figure patch. I think that is a bug. The following works:
See the result:

jenshnielsen commentedon Aug 15, 2017
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
to do the trick
HDembinski commentedon Aug 15, 2017
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 commentedon Aug 15, 2017
Hi Jens, sorry, I was not reading closely enough what you wrote.
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 commentedon Aug 15, 2017
Ok I assumed that the attached image was the broken behaviour
anntzer commentedon 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.#rrggbbaaformat, introduced in #6382); #9038 adds support for 4-tuples.HDembinski commentedon Aug 15, 2017
@anntzer Thanks for the related PR :). I take whatever works, but I think that setting
savefig.facecolorto get the desired result is not intuitive.tacaswell commentedon Aug 15, 2017
Hmm, we change
savefig.dpito 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).[-]Feature request: rcParams option to permanently make figure background transparent but not axis background[/-][+]DOC: better document rcParams in savefig.* grouping[/+]HDembinski commentedon Aug 16, 2017
@tacaswell I read about
savefig.transparentbut 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 commentedon Aug 16, 2017
@anntzer I can confirm that this code
produces exactly what I want. Thanks! 😄
Following your advice as I understood it, I tried to add this line to my matplotlibrc:
But I then get this error:
HDembinski commentedon Aug 16, 2017
Okay, looking into the relevant code myself helps ;)
It works with the current matplotlib 2.0.2, if I put this line into matplotlibrc
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 commentedon Aug 16, 2017
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.HDembinski commentedon Aug 23, 2017
This is fantastic. Thanks everyone!