Transparent background for figures

조회 수: 1,156 (최근 30일)
Daniel Shub
Daniel Shub 2011년 10월 31일
댓글: Alexis 2023년 11월 15일
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
Fangjun Jiang 2011년 10월 31일
+1. I'd like to know about 'transparent'.
Jose Sosa Lopez
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');

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

채택된 답변

Oliver Woodford
Oliver Woodford 2011년 11월 14일
You can edit the eps file generated by print, and remove the line which creates the background rectangle, thus making the background transparent.
Or you can use the latest version of export_fig, which will do this for you when the -transparent option is specified, and will allow you to export to pdf as well.
  댓글 수: 2
Daniel Shub
Daniel Shub 2011년 11월 14일
Will take a look at how you edit the eps file in export_fig (and will likely accept your answer once I do). I still don't understand why the change in MATLAB was made.
Daniel Shub
Daniel Shub 2011년 12월 9일
I finally got a chance to dig around in export_fig. For those silly enough not to use export_fig, the line in the eps file can be found with: regexp(l, ' *0 +0 +\d+ +\d+ +rf *[\n\r]+', 'start').

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

추가 답변 (5개)

Jan
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
Daniel Shub 2011년 10월 31일
It seems odd that they are taking away functionality without providing a replacement. This usually means you are doing something odd. In this case, I don't think I am. My guess is that there will be a replacement by the time it is obsolete.
Oliver Woodford
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
Fangjun Jiang 2011년 10월 31일
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
N Kando
N Kando 2016년 12월 19일
Maybe need to do both?
Sri Adiyanti
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
Samaneh Manavi 2021년 9월 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
Richard Quist 2021년 9월 3일
편집: Richard Quist 2021년 9월 3일
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
Sri Adiyanti 2021년 10월 5일
Thanks Richard, looks promising.
I can't use exportgraphics as in print in my m.script?
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
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:
  1. remove the Spirograph\Yanti\Systematic\ from your MATLAB path (see the rmpath command)
  2. rename Spirograph\Yanti\Systematic\exportgraphics.m to something else

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


Farrar Telford
Farrar Telford 2023년 5월 10일
You should add axis off. this works for me:
set(gca, 'Color', 'None');
axis off
  댓글 수: 1
Alexis
Alexis 2023년 11월 15일
Nice one, this worked for me too!

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

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by