Save OR open figure with transparent background

18 views (last 30 days)
Hello,
I need to save my pie charts without white background behind it (transparent), I was searched dozens of webpages and tried:
set(gcf, 'color', 'none');
% and
set(gca 'color', 'none');
But not works for me. I even used export_fig from FEX in this syntax:
export_fig (ax, ['filename' num2str(k) '.png']);
But I got this error:
Unrecognized function or variable 'using_hg2'.
Error in export_fig>parse_args (line 1391)
options.aa_factor = 1 + 2 * (~(using_hg2(fig) && isAA) |
(options.renderer == 3));
Error in export_fig (line 330)
[fig, options] = parse_args(nargout, fig, varargin{:});
Here is all my code,
clf
f = figure();
ax = axes();
p = pie(ax, ones(1,5));
t = p(2:2:end);
p = p(1:2:end);
delete(t)
s = {'CC', 'ME', 'NU', 'BI', 'IA'};
for k=1:size(davar1, 1)
for i=1:numel(s)
switch davar1(k,:).([s{i} '_CHECK']) % I even tried insert (j) here and do it in the for loop but error says Index exceeds the number of array elements (2).
case 'New York'
p(i).FaceColor = 'g';
case 'California'
p(i).FaceColor = 'y';
case 'Illinois'
p(i).FaceColor = 'r';
case 'Texas '
p(i).FaceColor = 'k';
case 'Ohio'
p(i).FaceColor = 'b';
case 'North Carolina'
p(i).FaceColor = 'c';
case 'Tennessee'
p(i).FaceColor = 'w';
end
end
exportgraphics(ax, ['filename' num2str(k) '.png']);
end
I saw some people say save figure as .png then go to windows Microsoft Office PowerPoint and make your picture background transparent but I have over 200 images and it would be awesome if Matlab can do it. Please guid me how I can export my figure in transparent mode.
Thank you all

Accepted Answer

Ameer Hamza
Ameer Hamza on 12 Apr 2020
Edited: Ameer Hamza on 12 Apr 2020
Call export_fig with additional inputs
export_fig('filename', '-dpng', '-transparent', '-r300'); % -r300 is the PPI value, default resolution is low
  4 Comments
John Mungall
John Mungall on 21 Jan 2021
This was remarkably helpful. I am very grateful.
In my case, the last three lines of my test were:
scatter(x,y,sz,c,'filled');
filename = 'scattertransparent2';
export_fig(filename, '-dpng', '-transparent', '-r300'); % -r300 ...
After some "jiggling" with Word Size and Position, this let me produce a scatter plot overylying a jpg map with the map being situated nicely inside the plot axes. What a relief...
Yuan Zhang
Yuan Zhang on 2 Jun 2021
Thanks Ameer, your code is really helpful!

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!