0

I'd like the background of my matplotlib plots to be transparent in my IPython notebook. This may sound silly because the notebook itself defaults to a white background but:

1) I use a solarized background and

2) more importantly, I want them to be transparent for when I embed the notebook directly into my blog via nbconvert.

It's easy enough to use something like savefig('file', transparent=True) , but I'm not saving the figures, I am displaying them inline (by calling IPython with ipython notebook --matplotlib inline.

I've been playing around with the IPython notebook configuration file, especially with c.InlineBackend.rc. For example, I upgraded to the dev version of matplotlib to get access to its new savefig.transparent rcParam, and tried configuring that with c.InlineBackend.rc = {'savefig.transparent': True}, but as expected it only affects plots saved with savefig.

Note that I am using the recent IPython 2.0 release. This must be possible somehow, right? Any light that you can shed would be appreciated.

4
  • the figures are displayed via pngs which are embedded directly in the html. You are saving the figures, ipython is just hiding it from you. You probably have to dig into the ipython code to find where the do the save and add the ability to pass parameters through.
    – tacaswell
    Commented Apr 7, 2014 at 12:35
  • I feel that your second approach should work (setting the ipython rc parameter).
    – tillsten
    Commented Apr 7, 2014 at 22:48
  • @tillsten, my suspicion is that this means IPython is not calling savefig directly when it uses matplotlib inline. Maybe it is using some lower level function? I'll poke around and try to find out, but hopefully someone more knowledgable about these things knows off the top of their head.
    – Spencer
    Commented Apr 8, 2014 at 16:45
  • I posted an issue on the ipython github page, maybe they know more. I tried every i way i could think of (figure constructor, rcparmas, ipython config) without being able to set to figure facecolor.
    – tillsten
    Commented Apr 8, 2014 at 22:39

2 Answers 2

3

Just to follow up, the issue opened on Github by tillsten has been patched so something like this:

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

should work now after you update IPython. Three cheers for open source.

0

The inline plots are html objects (<img>) with class ui-resizable. So you can change their default behavior by customizing the CSS for your notebooks:

  1. locate your settings for notebooks: in a terminal, type

    ipython locate
    
  2. in the indicated directory, go to subdir profile_default\static\custom (or any profile you want to use instead)

  3. edit or create a CSS file named custom.css

  4. put this in it:

    img.ui-resizable
    {
    opacity:0.4;
    }
    

Close your notebooks, kill IPython and restart it (so that it recreates the served files).

It should work with exported notebooks, as long as you export them as html and you change the css there too.

It's not exactly what you want, but it does the job.

2
  • Unfortunately, this isn't at all what I want. This makes the whole image transparent, including the content, and does nothing to change the white background.
    – Spencer
    Commented Apr 7, 2014 at 21:38
  • Sure, I now understand that you only want the background to be (fully) transparent.
    – Taar
    Commented Apr 8, 2014 at 8:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.