Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Export figure with transparent background

3,149 views
Skip to first unread message

Bruno Luong

unread,
Jun 3, 2008, 12:50:19 PM6/3/08
to
Hello,

is there any way to export the figure into an appropriates
graphic format for transparent background (MATLAB default is
white)?

The finale purpose is the import it into a power point
presentation using the ppt default slide background.

Thank you,

Bruno

Bruno Luong

unread,
Jun 9, 2008, 5:35:03 AM6/9/08
to
The solution is:

using menu: Preferences -> Figure copy template -> Copy
options -> Check "transparent background" to on

Set the 'Color' property of all axes of the figure to 'none'

Use the menu 'Edit' -> 'Copy figure' to copy the graphic
into the clipboard, then past it to the powerpoint slide.

Bruno

us

unread,
Jun 9, 2008, 6:07:02 AM6/9/08
to
"Bruno Luong":
<SNIP ML-to-MSoffice...

> ...


> Use the menu 'Edit' -> 'Copy figure' to copy the graphic
> into the clipboard, then past it to the powerpoint slide

a note: you will get even better results if you do this

peaks(128);
print -dmeta -r400;
% - NO file name
% - -rX depends on your memory
% then paste into your app

just a thought
us

jeffrey

unread,
Nov 16, 2009, 7:35:19 PM11/16/09
to
THIS DOES NOT WORK FOR XY PLOTS.
The steps specified work on a "background" which excludes the actual xy plot area: the "background" consists only of the surrounding area.
How do we set the background of the entire figure to transparent?
JLC


"Bruno Luong" <b.l...@fogale.fr> wrote in message <g2itg7$ino$1...@fred.mathworks.com>...

Oliver Woodford

unread,
Nov 17, 2009, 5:36:02 AM11/17/09
to

Yes. Download export_fig (http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig). Now try the following:

peaks;
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
export_fig test.png

Now open test.png in your ppt presentation and voila! Not only anti-alised graphics (which are optional, btw), but also beautifully alpha-blended too. It even works with semi-transparent patches.

HTH,
Oliver

Bruno Luong

unread,
Nov 17, 2009, 7:45:19 AM11/17/09
to
"jeffrey " <cip...@wai.com> wrote in message <hdsr47$kuq$1...@fred.mathworks.com>...

> THIS DOES NOT WORK FOR XY PLOTS.
> The steps specified work on a "background" which excludes the actual xy plot area: the "background" consists only of the surrounding area.
> How do we set the background of the entire figure to transparent?

Figure Color needs to bet set to 'none', did you do that?

Bruno

Paul Shoemaker

unread,
Dec 2, 2009, 1:41:20 PM12/2/09
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <hdu5su$qfs$1...@fred.mathworks.com>...

In the case of xy plots, the culprit is likely the renderer used to display the plot, which is chosen automatically by Matlab based on the data being displayed. The "painter" that is likely being used for your xy plots does not respect transparency settings. See the following link for a description of the renderers in Matlab.

http://www.oit.uci.edu/dcslib/matlab/matlab-v60/help/techdoc/printing/printfi2.html

Fortunately, the rendering engine can be changed on the fly by the user. Here's what I used, with success, to export plots to PowerPoint using the actxserver:

% Setup the plot & figure properties prior to export
set(gcf,'renderer','OpenGL'); % Sets renderer to OpenGL


set(gcf,'color','none');
set(gcf,'color','none');

print -dmeta -r400; % copy figure to clipboard in metafile format at quality of 400
% Now export to ppt (assumes a ppt slide handle named "newSlide" has already been created) and set transparency
imageObject = invoke(newSlide.Shapes,'paste'); % paste clipboard image to ppt
set(imageObject.PictureFormat,'TransparentBackground','msoTrue');
set(imageObject.PictureFormat,'TransparentColor','16777215'); % Set transparency color to while
set(imageObject.Fill,'Transparency',1);
set(imageObject.Fil,'Visible','msoFalse');

As I said, the above worked for me, though this was after extensive experimentation so some of the above lines may not be necessary. Feel free to experiment with the above, especially if you want to change what color is white.

Paul Shoemaker

unread,
Dec 2, 2009, 1:42:20 PM12/2/09
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <hdu5su$qfs$1...@fred.mathworks.com>...

In the case of xy plots, the culprit is likely the renderer used to display the plot, which is chosen automatically by Matlab based on the data being displayed. The "painter" that is likely being used for your xy plots does not respect transparency settings. See the following link for a description of the renderers in Matlab.

Matt Fig

unread,
Dec 2, 2009, 2:16:18 PM12/2/09
to
This is one of the biggest PIAs of using MATLAB. Just yesterday I wanted to export a plot as a .png including the background color of the figure surrounding the axis. I tried setting the "use figure backgroundcolor" option in the copy options, but this changed nothing. I remember there was a trick to getting this done in v6.5, but I couldn't figure out what it was. I also tried using all of the different renderers, but to no avail.
Finally I tried Oliver's export_fig function, which worked as far as the backgroundcolor. Unfortunately it messed up the arrows and text boxes on the figure (Shifted them around, made it so that the text box border overlapped the text, etc.)

I sincerely wish TMW would make this feature WISIWYG at *some* point! I ended up just using the white-background in my presentation, which is NOT what I wanted AT ALL. :-(

Bummed....

Oliver Woodford

unread,
Dec 3, 2009, 4:33:01 AM12/3/09
to
"Matt Fig" wrote

> Finally I tried Oliver's export_fig function, which worked as far as the backgroundcolor. Unfortunately it messed up the arrows and text boxes on the figure (Shifted them around, made it so that the text box border overlapped the text, etc.)

Matt, I'd be grateful if you could email me the figure so I can take a look. It's useful to have failure cases.

Also, I suggest you try using export_fig with the zbuffer and painters renderers if it's not working well with the default (opengl for bitmaps). Just pass in the option -zbuffer or -painters. Make sure you have the latest version of export_fig though, as this functionality was only added recently.

Oliver

Claus Becker

unread,
Dec 3, 2009, 1:01:19 PM12/3/09
to
Reading this posting with great interest. Thanks.
Say, you wrote: % Now export to ppt (assumes a ppt slide handle named "newSlide" has already been created) ...
Could you share how you created the handle to the ppt ? Is it possible to drop slides into ppt as you go along? The clipboard will only hold one graph.

Thanks,

Claus


"Paul Shoemaker" <psho...@hotmail.com> wrote in message <hf6cec$ml7$1...@fred.mathworks.com>...

Matt Fig

unread,
Dec 3, 2009, 1:01:19 PM12/3/09
to
"Oliver Woodford" <o.j.woo...@cantab.net> wrote in message

Will do Oliver. I will have to change some things on the plot before I can let you see it (I am sure you understand that!), so I might not get to it till later. Also, I was using the latest version because I just downloaded it two days ago.

Matt

Paul

unread,
Dec 3, 2009, 2:00:30 PM12/3/09
to
On Dec 3, 12:01 pm, "Claus Becker" <gelbkre...@yahoo.com> wrote:
> Reading this posting with great interest. Thanks.Say, you wrote: % Now export to ppt (assumes a ppt slide handle named "newSlide" has already been created) ...

>
> Could you share how you created the handle to the ppt ? Is it possible to drop slides into ppt as you go along? The clipboard will only hold one graph.
>
> Thanks,
>
> Claus
>
>
>
> "Paul Shoemaker" <pshoe...@hotmail.com> wrote in message <hf6cec$ml...@fred.mathworks.com>...
> > "Bruno Luong" <b.lu...@fogale.findmycountry> wrote in message <hdu5su$qf...@fred.mathworks.com>...
> > > "jeffrey " <cipo...@wai.com> wrote in message <hdsr47$ku...@fred.mathworks.com>...

> > > > THIS DOES NOT WORK FOR XY PLOTS.
> > > > The steps specified work on a "background" which excludes the actual xy plot area: the "background" consists only of the surrounding area.
> > > > How do we set the background of the entire figure to transparent?
>
> > > Figure Color needs to bet set to 'none', did you do that?
>
> > > Bruno
>
> > In the case of xy plots, the culprit is likely the renderer used to display the plot, which is chosen automatically by Matlab based on the data being displayed.  The "painter" that is likely being used for your xy plots does not respect transparency settings.  See the following link for a description of the renderers in Matlab.
>
> >http://www.oit.uci.edu/dcslib/matlab/matlab-v60/help/techdoc/printing...

>
> > Fortunately, the rendering engine can be changed on the fly by the user.  Here's what I used, with success, to export plots to PowerPoint using the actxserver:
>
> > % Setup the plot & figure properties prior to export
> > set(gcf,'renderer','OpenGL'); % Sets renderer to OpenGL
> > set(gcf,'color','none');
> > set(gcf,'color','none');
> > print -dmeta -r400; % copy figure to clipboard in metafile format at quality of 400
> > % Now export to ppt (assumes a ppt slide handle named "newSlide" has already been created) and set transparency
> > imageObject = invoke(newSlide.Shapes,'paste'); % paste clipboard image to ppt
> > set(imageObject.PictureFormat,'TransparentBackground','msoTrue');
> > set(imageObject.PictureFormat,'TransparentColor','16777215'); % Set transparency color to while
> > set(imageObject.Fill,'Transparency',1);
> > set(imageObject.Fil,'Visible','msoFalse');
>
> > As I said, the above worked for me, though this was after extensive experimentation so some of the above lines may not be necessary.  Feel free to experiment with the above, especially if you want to change what color is white.

Claus, I will post the code you requested when I return to the office
next Tuesday. Yes, it is possible to paste multiple figures to ppt as
you go along by using the actxserver functionality in Matlab.

Jane

unread,
Dec 4, 2009, 12:43:18 AM12/4/09
to
"Bruno Luong" <b.l...@fogale.fr> wrote in message <g23sob$e50$1...@fred.mathworks.com>...

A totally different approach...

I've always just saved the image with the default white background and then in the picture toolbar in PowerPoint you can set a transparent colour by pushing the button in the menu and then clicking on an area of the image which is the colour you want to make transparent.

It works for me, and seems a little simplier than some of the suggestions here.

Oliver Woodford

unread,
Dec 4, 2009, 4:47:02 AM12/4/09
to
"Jane " wrote:
> A totally different approach...
>
> I've always just saved the image with the default white background and then in the picture toolbar in PowerPoint you can set a transparent colour by pushing the button in the menu and then clicking on an area of the image which is the colour you want to make transparent.
>
> It works for me, and seems a little simplier than some of the suggestions here.

Downsides to this are that you get non-anti-aliased bitmap graphics, you can't have semi-transparent patches over a transparent background, and also any objects that are the same colour as the background will be made transparent too.

Also, you're saving to png (or other bitmap), then opening it in powerpoint, then setting the transparent colour. If you switch to printing to png using export_fig (my approach: export_fig out.png) then you miss out the last step. If you follow Us' approach (print -dmeta -r400) it's even easier as you don't need to open a file - just paste.

Oliver

Jane

unread,
Dec 4, 2009, 4:10:36 PM12/4/09
to
"Oliver Woodford" <o.j.woo...@cantab.net> wrote in message <hfalqm$oqp$1...@fred.mathworks.com>...

I agree your method is proably better. It's just that mine is easier and is usually ok quality wise for me.
>
> Oliver

Paul Shoemaker

unread,
Dec 8, 2009, 12:02:03 PM12/8/09
to
Paul <psho...@gmail.com> wrote in message <4cc984f2-5797-4dab...@f20g2000vbl.googlegroups.com>...

> On Dec 3, 12:01?pm, "Claus Becker" <gelbkre...@yahoo.com> wrote:
> > Reading this posting with great interest. Thanks.Say, you wrote: % Now export to ppt (assumes a ppt slide handle named "newSlide" has already been created) ...
> >
> > Could you share how you created the handle to the ppt ? Is it possible to drop slides into ppt as you go along? The clipboard will only hold one graph.
> >
> > Thanks,
> >
> > Claus
> >
> >
> >
> > "Paul Shoemaker" <pshoe...@hotmail.com> wrote in message <hf6cec$ml...@fred.mathworks.com>...
> > > "Bruno Luong" <b.lu...@fogale.findmycountry> wrote in message <hdu5su$qf...@fred.mathworks.com>...
> > > > "jeffrey " <cipo...@wai.com> wrote in message <hdsr47$ku...@fred.mathworks.com>...
> > > > > THIS DOES NOT WORK FOR XY PLOTS.
> > > > > The steps specified work on a "background" which excludes the actual xy plot area: the "background" consists only of the surrounding area.
> > > > > How do we set the background of the entire figure to transparent?
> >
> > > > Figure Color needs to bet set to 'none', did you do that?
> >
> > > > Bruno
> >
> > > In the case of xy plots, the culprit is likely the renderer used to display the plot, which is chosen automatically by Matlab based on the data being displayed. ?The "painter" that is likely being used for your xy plots does not respect transparency settings. ?See the following link for a description of the renderers in Matlab.
> >
> > >http://www.oit.uci.edu/dcslib/matlab/matlab-v60/help/techdoc/printing...
> >
> > > Fortunately, the rendering engine can be changed on the fly by the user. ?Here's what I used, with success, to export plots to PowerPoint using the actxserver:

> >
> > > % Setup the plot & figure properties prior to export
> > > set(gcf,'renderer','OpenGL'); % Sets renderer to OpenGL
> > > set(gcf,'color','none');
> > > set(gcf,'color','none');
> > > print -dmeta -r400; % copy figure to clipboard in metafile format at quality of 400
> > > % Now export to ppt (assumes a ppt slide handle named "newSlide" has already been created) and set transparency
> > > imageObject = invoke(newSlide.Shapes,'paste'); % paste clipboard image to ppt
> > > set(imageObject.PictureFormat,'TransparentBackground','msoTrue');
> > > set(imageObject.PictureFormat,'TransparentColor','16777215'); % Set transparency color to while
> > > set(imageObject.Fill,'Transparency',1);
> > > set(imageObject.Fil,'Visible','msoFalse');
> >
> > > As I said, the above worked for me, though this was after extensive experimentation so some of the above lines may not be necessary. ?Feel free to experiment with the above, especially if you want to change what color is white.

>
> Claus, I will post the code you requested when I return to the office
> next Tuesday. Yes, it is possible to paste multiple figures to ppt as
> you go along by using the actxserver functionality in Matlab.

Claus,

As promised, here is the code to paste to ppt. The approach I outline allows you to paste to ppt without having to save figure as file and also sets the transparency for you automatically. This is helpful for jobs that export a large number of figures. Note that the below code is very stripped down. You could easily modify it to paste to existing ppt or the currently open ppt. Also, you could put the image editing portion in a for loop to paste multiple images, even multiple per page, with ease.

Also, note that this requires that the user has Office installed on their machine. In my testing, it seems to work with both Office 2003 and 2007.


[myFile myPath] = uiputfile('*.ppt','Create New PPT File'); % New ppt file. Modify if you want to append to existing. Remove if you want to use current active presentation (see exportPreso comment below)
filename = fullfile(myPath,myFile);
pptserver = actxserver('PowerPoint.Application'); % create ppt actx server
exportPreso = invoke(pptserver.Presentations,'Add'); % Open new presentation. To append to existing ppt, use 'open'. To append to currently active ppt, use exportPreso = get(pptserver,'ActivePresentation');
slideCount = get(exportPreso.Slides,'Count'); % Get page count of preso
slideCount = int32(double(slideCount)+1); % find the 'next' preso page
newSlide = invoke(exportPreso.Slides,'Add',slideCount,11); % create new slide
slideWidth = get(exportPreso.PageSetup,'SlideWidth'); % get slide width
slideHeight = get(exportPreso.PageSetup,'SlideHeight'); % get slide height
set(newSlide.Shapes.Title.TextFrame.TextRange,'Text',' '); % set slide title to blank
% Example plot below. For export of multiple plots, the below should be put in a for-loop. To
% paste multiple figures to each slide, modify the image size and position
% accordingly)
figure
plot(1:.1:10,sin(1:.1:10));
oldPosition = get(gca,'position');
oldRenderer = get(gcf,'renderer');
oldGCFcolor = get(gcf,'color');
oldGCAcolor = get(gca,'color');
set(gca,'position',get(gca,'outerposition')-get(gca,'tightinset')*[-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]); % make plot image 'tight' and remove whitespace. Seems to behave differently in Office 2007
set(gcf,'Renderer','OpenGL'); % change renderer to openGL


set(gcf,'color','none');

set(gca,'color','none');

print(gcf,'-dmeta','-r400'); % copy figure image to clipboard in meta format
set(gca,'position',oldPosition); % change figure properties back
set(gcf,'Renderer',oldRenderer);
set(gcf,'color',oldGCFcolor);
set(gca,'color',oldGCAcolor);
imageObject = invoke(newSlide.Shapes,'paste'); % page image to the new slide
set(imageObject.PictureFormat,'TransparentBackground','msoTrue'); % Set picture to transparent
set(imageObject.PictureFormat,'TransparencyColor','16777215'); % Set whites to be transparent
set(imageObject.Fill,'Transparency',1); % Fill is transparent
set(imageObject.Fill,'Visible','msoFalse'); % make fill invisible
set(imageObject,'width',0.8*slideWidth); % set size of pasted image
imageWidth = get(imageObject,'width');
imageHeight = get(imageObject,'height');
imageTop = 1.2*(slideHeight-imageHeight)/2;
imageLeft = (slideWidth-imageWidth)/2;
set(imageObject,'Top',imageTop,'Left',imageLeft); % set position of image in ppt
invoke(exportPreso,'SaveAs',filename,1); % Assumes new file. Modify to use an existing file. Remove if pasting to currently active preso
invoke(exportPreso,'Close'); % Remove if pasting to currently active preso

Glen Blenkinsop

unread,
Aug 6, 2012, 9:59:17 AM8/6/12
to
"Bruno Luong" <b.l...@fogale.fr> wrote in message <g23sob$e50$1...@fred.mathworks.com>...
Hi,
Try this, it works for me:

axes1 = axes('Parent',figure,'Color','none');
hold(axes1,'all');
plot(data)

Glen

Jason Garcia

unread,
Jan 25, 2013, 9:00:08 AM1/25/13
to
"Bruno Luong" <b.l...@fogale.fr> wrote in message <g23sob$e50$1...@fred.mathworks.com>...
Try this:

function PasteTransprntFig(filespec)
%% Draw Plot
figure
x = linspace(0,2*pi);
y = rand(1,100);
polar(x,y)

%% Adjust Figure Settings
%All that's needed is gcf Color settings and editmenufcn() operation.
set(gcf, 'PaperPosition', [0.25 0.25 8.0 6.0])
set(gcf, 'Position' , [1 1 459 362])
set(gcf, 'Color' , 'None')

set(gca, 'Position' , [0 0 1 1])

editmenufcn(gcf, 'EditCopyFigure')

%% PowerPoint Commands (pulled from saveppt.m)
ppt = actxserver('PowerPoint.Application'); %Start an ActiveX session with PowerPoint.

if ~exist(filespec, 'file')
op = invoke(ppt.Presentations, 'Add'); %Create new presentation.
else
op = invoke(ppt.Presentations, 'Open', filespec, [], [], 0); %Open existing presentation.
end

slide_count = get(op.Slides, 'Count'); %Get current number of slides.
slide_count = int32(double(slide_count) + 1);
new_slide = invoke(op.Slides, 'Add', slide_count, 11); %Add a new slide (with title object).

pic1 = invoke(new_slide.Shapes, 'Paste'); %Paste the contents of the Clipboard.

%% Exiting Commands
if ~exist(filespec,'file')
invoke(op, 'SaveAs', filespec,1); %Save file as new.
else
invoke(op, 'Save'); %Save existing file.
end
invoke(op, 'Close'); %Close the presentation window.
invoke(ppt, 'Quit'); %Quit PowerPoint.
end

Some additional positioning commands such as the ones below may be useful too:
%% Get Height and Width of Slide
slide.H = op.PageSetup.SlideHeight;
slide.W = op.PageSetup.SlideWidth;

%% Set Position and Size of Image
set(pic1, 'LockAspectRatio', 'msoFalse')
set(pic1, 'Left' , leftmostpos);
set(pic1, 'Top' , topmostpos);
set(pic1, 'Height', pic_height);
set(pic1, 'Width' , pic_width);

sout...@gmail.com

unread,
Apr 2, 2019, 10:33:27 AM4/2/19
to
hey,export_fig function works but the outcome image resolution is really bad.Is there other command in addition to it?
0 new messages