From: Andrew S. <str...@as...> - 2004-05-20 21:27:54
|
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) 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 |
From: John H. <jdh...@ac...> - 2004-05-21 14:43:25
|
>>>>> "Andrew" == Andrew Straw <str...@as...> writes: Andrew> Hi all, I'd like use matplotlib to draw shapes on a Andrew> transparent background using the Agg backend to save .png Andrew> files. Is this possible? Andrew> I've tried Andrew> 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. 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 Andrew> This seems to get halfway, but there's still (opaque) Andrew> white drawn in the background. Andrew> Is there a "recommended way", and, if so, what is it? Andrew> Otherwise, can we improve matplotlib to allow drawing on Andrew> transparent backgrounds? Andrew> Cheers! Andrew Andrew> ------------------------------------------------------- Andrew> This SF.Net email is sponsored by: Oracle 10g Get Andrew> certified on the hottest thing ever to hit the Andrew> market... Oracle 10g. Take an Oracle 10g class now, and Andrew> we'll give you the exam FREE. Andrew> http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click Andrew> _______________________________________________ Andrew> Matplotlib-users mailing list Andrew> Mat...@li... Andrew> https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: Andrew S. <str...@as...> - 2004-05-21 21:02:14
|
John Hunter wrote: >>>>>>"Andrew" == Andrew Straw <str...@as...> writes: >>>>>> >>>>>> > > Andrew> Hi all, I'd like use matplotlib to draw shapes on a > Andrew> transparent background using the Agg backend to save .png > Andrew> files. Is this possible? > > Andrew> I've tried > > Andrew> 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 |
From: John H. <jdh...@ac...> - 2004-05-21 21:23:38
|
>>>>> "Andrew" == Andrew Straw <str...@as...> writes: Andrew> By "working for you", do you mean that you don't have a Andrew> red face? Yes, that's fine, but if you read my initial Andrew> email, I said "there's still (opaque) white drawn in the Andrew> background" for this case. What do you get? I predict Andrew> that you do. Oh, I did misread. Yes, I was getting rid of the red but didn't notice the white background of the canvas. This does, as you noted, show up whether or not you set alpha to zero and whether or not you draw the figure frame. The reason is, when I initialize the agg buffer (src/_backend_agg.cpp) I do rendererBase->clear(agg::rgba(1, 1, 1)); which sets a default background of white with a default alpha of 1. I just tested the following change to line 46: rendererBase->clear(agg::rgba(1, 1, 1, 0)); Ie, manually set the alpha of the background color to zero, and it worked as expected with your example. Now the background is truly transparent. I also checked this in CVS but you can just change the one line to get the same result. JDH |
From: Andrew S. <str...@as...> - 2004-05-22 01:24:17
|
>Ie, manually set the alpha of the background color to zero, and it >worked as expected with your example. Now the background is truly >transparent. I also checked this in CVS but you can just change the >one line to get the same result. > > > Thanks! It works great! Matplotlib rocks! Andrew |