Skip to content

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

Closed

Description

@HDembinski

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

Activity

  1. HDembinski commented on Aug 14, 2017

    @HDembinski
    Author

    Related issue: #2384

  2. HDembinski commented on Aug 14, 2017

    @HDembinski
    Author

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

  3. tacaswell commented on Aug 14, 2017

    @tacaswell
    Member

    The facecolor can be an RGBA value so setting

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

    should do what you want?

  4. HDembinski commented on Aug 15, 2017

    @HDembinski
    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))
    
  5. HDembinski commented on Aug 15, 2017

    @HDembinski
    Author

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

  6. HDembinski commented on Aug 15, 2017

    @HDembinski
    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

  7. jenshnielsen commented on Aug 15, 2017

    @jenshnielsen
    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

  8. HDembinski commented on Aug 15, 2017

    @HDembinski
    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.

  9. HDembinski commented on Aug 15, 2017

    @HDembinski
    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.

  10. jenshnielsen commented on Aug 15, 2017

    @jenshnielsen
    Member

    Ok I assumed that the attached image was the broken behaviour

  11. anntzer commented on Aug 15, 2017

    @anntzer
    Contributor

    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.

  12. HDembinski commented on Aug 15, 2017

    @HDembinski
    Author

    @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.

  13. tacaswell commented on Aug 15, 2017

    @tacaswell
    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).

  14. added this to the milestone on Aug 15, 2017
  15. 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
  16. HDembinski commented on Aug 16, 2017

    @HDembinski
    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.

  17. HDembinski commented on Aug 16, 2017

    @HDembinski
    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))
    
  18. HDembinski commented on Aug 16, 2017

    @HDembinski
    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.

  19. HDembinski commented on Aug 16, 2017

    @HDembinski
    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.

  20. modified the milestones: , on Aug 22, 2017
  21. HDembinski commented on Aug 23, 2017

    @HDembinski
    Author

    This is fantastic. Thanks everyone!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions