removing Figure background (or, making it transparent)

Hi all, I'd like use matplotlib to draw shapes on a

    > transparent background using the Agg backend to save .png
    > files. Is this possible?

    > I've tried

    > fig._figurePatch.set_alpha(0)

There is a vestigial limitation from the bad old days when we didn't
have an alpha channel. For polyons, there is no place holder for a
separate alpha channel for the edge and face. The way to do this
right is to simply replace all rgb instances throughout the library
with rgba, but this is a fair amount of work that touches the entire
library. What I am doing currently is sharing the alpha attribute
between edge and face for polygons.

So setting alpha=0 *should work. What backend and version are you
using? Rectangle calls gc.set_alpha before calling draw_polygon in
patches.Patch. In _backend_agg.RendererAgg.draw_polygon, the alpha
attribute for the face is taken from the edge which gets it from the
gc (around line 204 in _backend_agg). I say all this just because

  f = figure(1, facecolor='r')
  f._figurePatch.set_alpha(0.0)

does work for me using Agg and the latest matplotlib.

What backend and version are you using?

That said, the best way to do this is to add a frameon attribute for
figures, because this will be respected across backends including
those who don't understand alpha. This way you can say

f = figure(1, frameon=False)

I checked this into CVS.

JDH

···

from my read of the code *it should* work, and indeed, in my tests

    > This seems to get halfway, but there's still (opaque)
    > white drawn in the background.

    > Is there a "recommended way", and, if so, what is it?
    > Otherwise, can we improve matplotlib to allow drawing on
    > transparent backgrounds?

    > Cheers! Andrew

    > -------------------------------------------------------
    > This SF.Net email is sponsored by: Oracle 10g Get
    > certified on the hottest thing ever to hit the
    > market... Oracle 10g. Take an Oracle 10g class now, and
    > we'll give you the exam FREE.
    > http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
    > _______________________________________________
    > Matplotlib-users mailing list
    > Matplotlib-users@lists.sourceforge.net
    > matplotlib-users List Signup and Options

John Hunter wrote:

"Andrew" == Andrew Straw <strawman@...106...> writes:
           
   > Hi all, I'd like use matplotlib to draw shapes on a
   > transparent background using the Agg backend to save .png
   > files. Is this possible?

   > I've tried

   > fig._figurePatch.set_alpha(0)

There is a vestigial limitation from the bad old days when we didn't
have an alpha channel. For polyons, there is no place holder for a
separate alpha channel for the edge and face. The way to do this
right is to simply replace all rgb instances throughout the library
with rgba, but this is a fair amount of work that touches the entire
library. What I am doing currently is sharing the alpha attribute
between edge and face for polygons.

So setting alpha=0 *should work. What backend and version are you
using? Rectangle calls gc.set_alpha before calling draw_polygon in
patches.Patch. In _backend_agg.RendererAgg.draw_polygon, the alpha
attribute for the face is taken from the edge which gets it from the
gc (around line 204 in _backend_agg). I say all this just because
from my read of the code *it should* work, and indeed, in my tests

f = figure(1, facecolor='r')
f._figurePatch.set_alpha(0.0)

does work for me using Agg and the latest matplotlib.

By "working for you", do you mean that you don't have a red face? Yes, that's fine, but if you read my initial email, I said "there's still (opaque) white drawn in the background" for this case. What do you get? I predict that you do.

What backend and version are you using?

Agg, PS, CVS HEAD

That said, the best way to do this is to add a frameon attribute for
figures, because this will be respected across backends including
those who don't understand alpha. This way you can say

f = figure(1, frameon=False)

I checked this into CVS.

JDH

OK, here's a sample program (modified slightly from scatter_demo2.py:

#!/usr/bin/env python
import sys
from matplotlib.matlab import *
from data_helper import get_daily_data

fig = figure(1,frameon=False)

intc, msft = get_daily_data()

delta1 = diff(intc.open)/intc.open[0]

volume = (10*intc.volume[:-2]/intc.volume[0])**2
close = 0.003*intc.close[:-2]/0.003*intc.open[:-2]
p = scatter(delta1[:-1], delta1[1:], c=close, s=volume)
set(p, 'alpha', 0.75)
set(gca(), 'xticks', arange(-0.06, 0.061, 0.02))
set(gca(), 'yticks', arange(-0.06, 0.061, 0.02))
xlabel(r'\\Delta\_i', fontsize='x-large')
ylabel(r'\\Delta\_\{i\+1\}', fontsize='x-large',
       verticalalignment='center',
       horizontalalignment='right',
       rotation='horizontal'
       )
title(r'Volume and percent change')
grid(True)
gca().set_frame_on(0)
fig._figurePatch.set_alpha(0.0) # doesn't affect output
savefig('scatter_demo3',facecolor='b',edgecolor='g')
#show()

Now, with the Agg backend (saving to PNG), I still get a non-transparent white background. (The face and edgecolors were specified just to test if they show up, which they don't.) Also, with the PS backend, I get a white rectangle at figure size.

So, it looks like there's another white rectangle creeping in there somewhere... Can we find it and get rid of it?

Cheers!
Andrew