How do you save a figure with a trasparent background?
586 views (last 30 days)
Show older comments
How do you save a figure with a trasparent background? I want to save a figure as a png image that has a transparent background so I can put the image into PowerPoint and the PowerPoint graphic will show through the Matlab figure.
0 Comments
Answers (4)
jonas
on 2 Mar 2018
Edited: Stephen23
on 14 Mar 2018
5 Comments
jonas
on 14 Mar 2018
Sorry for the late response. If you did not solve it already perhaps you can upload the code and I will have a look.
The first option calls the current axis (gca), so it should be located after the figure. The other option should be at the beginning of the code.
DGM
on 12 Dec 2024
So that we have an example:
% a figure with a line plot
x = linspace(0,2*pi,100);
y = [sin(x); cos(x)];
plot(x,y)
xlim([0 2*pi])
% write a transparent PNG using FEX export_fig()
fname = 'output.png';
export_fig(fname,'-transparent',gcf)
% since it's a PNG with transparency,
% you must retrieve the alpha channel separately.
% most native tools (e.g. imshow) will not directly handle an RGBA image.
% for info on handling RGBA images within MATLAB, do a forum search
[inpict,~,alpha] = imread(fname);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1820683/image.png)
Note that this inserted image is transparent both within and outside the plot box. It just doesn't show since the page background is also white.
cui,xingxing
on 30 Aug 2021
since Matlab R2020a, use "exportgraphics", for example ,you can do like this
x = 0:.1:2*pi;
y = sin(x);
% save to transparented image
figure;
plot(x,y,'LineWidth',4);
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
exportgraphics(gcf,'transparent.eps',... % since R2020a
'ContentType','vector',...
'BackgroundColor','none')
7 Comments
Subba Reddy
on 22 Jan 2020
Non matlab way, Can use gimp software to remove white background from image, by selecting the background with color select tool, invert selection and copy. Then create new gimp window with background option as tranperancy, and paste it into that and export that as png. Not programmable but can be used to prepare images for presentation.
1 Comment
DGM
on 12 Dec 2024
Edited: DGM
on 12 Dec 2024
Even within GIMP, that's not a great way to approach the problem. While GIMP does provide antialiased selection tools, we don't have to deal with the fringing that they'll still cause.
% a figure with a line plot
x = linspace(0,2*pi,100);
y = [sin(x); cos(x)];
plot(x,y)
xlim([0 2*pi])
% take a screenshot of the figure
outpict = frame2im(getframe(gcf)); % RGB, uint8
% let's be lazy and just use MIMT color2alpha().
% this is the same as the GIMP plugin of the same name.
opc2a = color2alpha(outpict,255/255); % RGBA, uint8
% the color parameter is unit-scale and automatically expanded.
% since the interior and exterior of the plot box are 255 and 240 (in modern versions)
% the plot box is now 100% transparent,
% while the exterior area is slightly opaque.
% IPT imshow() doesn't handle RGBA images, but MIMT imshow2() does
imshow2(opc2a)
% most native tools don't handle RGBA images
% so it may help to split the color from the alpha
[colorout alphaout] = splitalpha(opc2a);
imwrite(colorout,'output2.png','alpha',alphaout)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1820684/image.png)
Again, the transparency in this image doesn't show up due to the white page background.
Otherwise, we can use other color-based selection techniques to do this programmatically in MATLAB without needing to use GIMP. There are lots of examples here on the forum.
W. Jeremy Morrison
on 18 Dec 2024
Edited: W. Jeremy Morrison
on 18 Dec 2024
This is my method to programatically generate transparent-background PNG files from Matlab figures. I use a Mac, but this should be the same process on windows.
Note: This method requires the inkscape command line tool. Installing this should be fairly easy.
(1) Generate the desired figure in Matlab and save it as an .eps vector graphic.
f = figure(fNum);
exportgraphics(f,[savePath '/' name '.eps'],...
BackgroundColor="none",ContentType="vector");
(2) Use inkscape command line tool to convert from .eps to .png and remove the .eps file. Use system() to work on the command line while in Matlab.
system([inkscapePath ' -d 600 ' savePath '/' name '.eps'...
' -o ' savepath '/' name '.png; rm ' savePath '/' name '.eps' ]);
Here inkscapePath pints to where the inkscape tool is installed. You can find this path by typing 'which inkscape' into your terminal. The -d flag of the inkscape tool conotrols the dots-per-inch, i.e. the resolution, of the converted .png file. The default is 96, which is pretty horrible.
Example on my Macbook:
% example that plots the standard normal distribution and converts it to
% a .png with transparent background
% kept the formatting the same as above for better understanding
% discretise and compute pdf values
xvec = linspace(-5,5,100);
pdfvec = normpdf(xvec);
% plot
f = figure(1); clf(1);
plot(xvec,pdfvec);
% set the x and y axes to invisible
f.CurrentAxes.Visible = 'off';
% export as .eps file to the current working directory
savePath = '.';
name = 'normpdf';
exportgraphics(f,[savePath '/' name '.eps'],...
BackgroundColor="none",ContentType="vector");
% use inkscape to convert to .png file
inkscapePath = '/opt/homebrew/bin/inkscape';
system([inkscapePath ' -d 600 ' savePath '/' name '.eps'...
' -o ' savepath '/' name '.png; rm ' savePath '/' name '.eps' ]);
Output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1821349/image.png)
0 Comments
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!