Save OR open figure with transparent background

조회 수: 13 (최근 30일)
BN
BN 2020년 4월 12일
댓글: Yuan Zhang 2021년 6월 2일
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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 12일
편집: Ameer Hamza 2020년 4월 12일
Call export_fig with additional inputs
export_fig('filename', '-dpng', '-transparent', '-r300'); % -r300 is the PPI value, default resolution is low
  댓글 수: 4
John Mungall
John Mungall 2021년 1월 21일
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 2021년 6월 2일
Thanks Ameer, your code is really helpful!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by