How to save figure with transparent background
Things have changed since the MATLAB 2014b release. The newly implemented graphics system (so called HG2, for Handle Graphics version 2) does much better in terms of transparency.
Now it saves transparency correctly to SVG at least!
It is disappointing but, MATLAB's default saveas
and print
commands cannot deal with transparent things very well. You'll have to save it with some background and then convert it either through imread
/imwrite
or some other tool.
There are some tools that might be helpful:
- Export fig http://www.mathworks.com/matlabcentral/fileexchange/23629
- svg export http://www.mathworks.com/matlabcentral/fileexchange/7401-scalable-vector-graphics-svg-export-of-figures
I prefer vector graphics, so use svg exports when transparency is needed. If indeed you have a bitmap, use imwrite(bitmapData, 'a.png', 'png', 'transparency', backgroundColor)
.
So I still wanted something simple that did not require me to install anything else (corporate pc not allowed :/). I stumbled upon this link, stating:
All you have to do is the following
1) In matlab file add the commands to format your figure with transparent background
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
and save or export the figure generated in eps format. (say Bspline.eps)
2) Open Bspline.eps in NotePad
3) Look at the first line. For example %!PS-Adobe-3.0 EPSF-3.0
. The last number 3.0 indicates the Postscript level. For level 3, search the string rf
. You will find in one line like this (four numbers followed by rf
)
0 0 3025 2593 rf %Comment that line using %.
(For level 2 search for string pr
instead of rf
)
Save the file.
Now you can use the eps file or you can convert it to pdf and then use it. Anyway it will have transparent background
Extra
For me it was two lines with re
and two lines, despite me having %!PS-Adobe-3.0 EPSF-3.0
just after each other. But the result was the Figure
was now transparent.