Transparent background for figures
古いコメントを表示
I get the following warning in r2011a
set(gcf, 'Color', 'None')
Warning: Setting the ColorSpec to 'none' for a figure will not be allowed in a future release.
I use the "none" color so that I can print pdf and eps files with transparent backgrounds. How are you supposed to set a transparent background?
2 件のコメント
Fangjun Jiang
2011 年 10 月 31 日
+1. I'd like to know about 'transparent'.
Jose Sosa Lopez
2020 年 7 月 8 日
if your graph is in figure 1, you can put:
figure (1)
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
採用された回答
その他の回答 (5 件)
Jan
2011 年 10 月 31 日
Digging in the source of print I've found the colornone command in 2011b. It has a help section, but is not included in the local or net docs.
But the warning appears also. The print command simply disables the warning:
warning('off','MATLAB:hg:ColorSpec_None')
This does not look very trustworthy.
2 件のコメント
Daniel Shub
2011 年 10 月 31 日
Oliver Woodford
2011 年 11 月 14 日
Note that this is not just a warning. The behaviour of print in R2011b has changed over R2011a. You cannot now save an eps or pdf with no background color, and TMW has provided no way around this (to my knowledge).
Fangjun Jiang
2011 年 10 月 31 日
1 投票
What do you mean transparent? Do you mean white background color? Would the pdf or eps file come out the same as set(gcf,'Color','None') as set(gcf,'Color',[1 1 1])?
I used to use whitebg(). But it's not really white background anymore since the default color is [.8 .8 .8]. whitebg() just turns it to [.3 .3 .3]. Same is the function colordef(). I
10 件のコメント
Daniel Shub
2011 年 10 月 31 日
Fangjun Jiang
2011 年 10 月 31 日
Okay, Thanks! When I tried set(gcf,'color','none') and export to an .eps file and then in MS Word, insert picture>From file, the front picture still blocks the view of the back picture. Why?
Jan
2011 年 10 月 31 日
On the screen you see the TIFF preview. Does the blocking background concern the printout or an exported PDF also?
Fangjun Jiang
2011 年 10 月 31 日
@Jan, I am using .eps file.
%%
plot(1:10);
set(gcf,'color','none');
saveas(gcf,'test.eps');
The test.eps only shows the axes. It doesn't show the margin we usually see in the MATLAB figure window.
Daniel Shub
2011 年 10 月 31 日
Fangjun Jiang
2011 年 10 月 31 日
PowerPoint is the same. Anyway, I certainly can understand the difference between transparent and white. Thanks!
Daniel Shub
2011 年 11 月 1 日
Fangjun Jiang
2011 年 11 月 1 日
It's still the same for me, in MS Word or PowerPoint. In your question, shout it be gca or gcf?
N Kando
2016 年 12 月 19 日
Maybe need to do both?
Sri Adiyanti
2021 年 4 月 15 日
I have trouble with preparing jpeg of plots with a transparent background (no colour).
I am using R2019a and can't produce figure with a transparent background when print it using print (gcf,...').
When using 'InvertHardcopy', 'off' it produces a grey background.
Any suggestion?
Samaneh Manavi
2021 年 9 月 1 日
1 投票
In the figure window, select property inspector by double clicking on your plot. There is a box styling section in which you can select the background color, by selecting none you can have it transparent. If you want this setting already in your code, you can select "Generate Code" in the file tab of the figure window and have all your manual settings in code.
Samaneh Manavi
Richard Quist
2021 年 9 月 3 日
編集済み: Richard Quist
2021 年 9 月 3 日
0 投票
You can use the exportgraphics command, which was introduced in R2020a, to easily generate output with a transparent background if the output format supports it. For example, the following code will export the plot to a PDF file with background transparency.
plot(rand(4));
exportgraphics(gca,'plot.pdf','BackgroundColor','none')
If you also want the background of the axes (the area behind the lines on the plot) to also be transparent in the exported file, set the axes color to 'none':
set(gca, 'color', 'none');
exportgraphics(gca,'plot2.pdf','BackgroundColor','none')
Another benefit of using exportgraphics is that the generated output is closely cropped around the exported content, eliminating excess "white space" around the plot.
2 件のコメント
Sri Adiyanti
2021 年 10 月 5 日
Thanks Richard, looks promising.
print(gcf, filename, '-djpeg', '-r1200');
exportgraphics(gcf, 'test.jpeg','Resolution',300)
It works with "print" commnd, but gave me this error for exportgraphics:
Execution of script exportgraphics as a function is not supported:
C:\Users\sadiy\OneDrive\Desktop\MATLAB\Spirograph\Yanti\Systematic\exportgraphics.m
Richard Quist
2021 年 10 月 5 日
@Sri Adiyanti: From your comment above, it looks like you have another function named exportgraphics (C:\Users\sadiy\OneDrive\Desktop\MATLAB\Spirograph\Yanti\Systematic\exportgraphics.m) that is shadowing (hiding) the exportgraphics function that is provided by MATLAB
Assuming you have R2020a or later, what output do you get when running the following command?
which -all exportgraphics
My guess is that will return both the MATLAB exportgraphics path as well as the path to the one in your Spirograph\Yanti\Systematic\ folder. If that's the case I can think of a couple of approaches to resolve the conflict:
- remove the Spirograph\Yanti\Systematic\ from your MATLAB path (see the rmpath command)
- rename Spirograph\Yanti\Systematic\exportgraphics.m to something else
Farrar Telford
2023 年 5 月 10 日
0 投票
You should add axis off. this works for me:
set(gca, 'Color', 'None');
axis off
1 件のコメント
Alexis
2023 年 11 月 15 日
Nice one, this worked for me too!
カテゴリ
ヘルプ センター および File Exchange で Printing and Saving についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!