MATLAB Answers

Figure export/print always has gray background with png/jpg

69 views (last 30 days)
Jacob
Jacob on 7 Jan 2013
Answered: Gaston on 5 Oct 2018
Whenever I export a figure in matlab to a png or jpg, the background is always gray. This is the case with using the 'print' and 'saveas' functions, as well as the fileexchange function export_fig. I have tried toggling the inverthardcopy and color figure settings to no avail.
This is very strange because it is a recent problem that seems to come out of nowhere. That is, i had no problems one day on my machine and the next day the same code could not switch the background color (white is what i want). In fact, exporting figures on my home computer works exactly as expected.
For example, the following code will save a figure with a gray background on my laptop but a white background on my home computer.
>> a = rand(100,2); >> plot(a); >> set(gcf,'inverthardcopy','off'); >> set(gcf,'color','w'); >> saveas(gcf,'test.png');
Similarly, if i set inverthardcopy to 'on' and dont change the background color, my home computer png will have a white background and my laptop will have a gray one.
I have noticed that the only way i can get the correct background color on a saved image is by setting inverthardcopy to 'off', manually changing the figure color, and saving as a .bmp file.
Any ideas on what setting may have been changed on my laptop to cause this?
Thanks,
Jacob

Answers (4)

Jan
Jan on 7 Jan 2013
Did you alter any default settings in startup.m? Compare this on bogth computers:
get(0, 'Default')
In addition there is a field in the preferences dialog:
Preferences->Figure Copy Template->Copy Options->
* Use figure color
* Force white background
* Transparent background
Do you have the same setting on both computers?

Chris
Chris on 15 Mar 2013
I had the same issue and removing the default figure color option did the job.
set(0,'DefaultFigureColor','remove')
  1 Comment
Karl
Karl on 3 Apr 2013
Worked for me: with one change:
set(0,'DefaultFigureColor',[1 1 1])

Sign in to comment.


Henry
Henry on 10 Jun 2014
So I have had this problem when every figure i exported using the openGL renderer has a black background which i did not want. I searched through the internet for a solution but never came across an answer. I thought I would post on here in case anyone else comes across this thread.
It appears that a setting has been unwillingly changed (although I dont know which one). My solution was to find the preferences directory by typing prefdir into the command line.
prefdir
Close matlab
Delete this folder
Re-open matlab.
The folder is recreated but not previous settings or history is used by matlab. It is quite annoying but it solved the issue with always having the black background with exported figures
  2 Comments
jimmydx
jimmydx on 16 Jun 2016
Thank you so much for this insight. I was able to solve my problem by deleting the prefdir. However, I only deleted the files that had been modified during the session where I started having problems. Of course it would be much more helpful to know 1) what triggered it and 2) what preference actually changed. Anyway, your solution saved me a huge headache.

Sign in to comment.


Gaston
Gaston on 5 Oct 2018
I had the same problem with saving a figure as a .eps file and none of the other answers solved my problem. What did work for me was replacing
saveas(gcf,'filename','eps');
by
print('filename','-depsc')

Community Treasure Hunt

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

Start Hunting!