0

If I print a figure in MATLAB the background of the plot gets rendered black instead of white, like this:

Image with wrong background

(But in the figure window of MATLAB it is white as it is supposed to be)
This is the code similar to the one used to print the figure(but will run copy-paste):

figure;
set(0,'DefaultAxesFontSize',13)
set(0,'DefaultTextFontSize',13)
set(gcf, 'Renderer', 'painters');
y = 1:100;
plot(y)
xlabel('Some X', 'FontSize', 14)
ylabel('Some Y', 'FontSize', 14)
title('Example', 'FontSize', 15)
legend('some function')
print(gcf,'test1.pdf','-dpdf')

If I add the following line, however, it works (but there a slight grey background in the areas around the plot, of course)

set(gcf, 'color', [0.99 0.99 0.99])

My MATLAB version is R2013a (8.1.0.604)

edit:
set(gcf, 'InvertHardCopy', 'off'); does not resolve the issue.

edit2:
The problem seams to be caused by the HG2-Update.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
rob
  • 2,904
  • 5
  • 25
  • 38
  • http://www.mathworks.com/help/matlab/creating_plots/changing-a-figures-settings.html#f3-99733 Try `InvertHardCopy` (haven't tested) – Yvon Aug 07 '14 at 15:42

2 Answers2

1

The copy-paste code actually works fine on my version (R2013a as well), but to ensure the background color stays what it appears to be like after print, use this:

set(gcf, 'InvertHardCopy', 'off');

You can look at the example in the matlab docs here under Setting the Background Color.

Thus, to get a different background color for your plot, use:

set(gcf, 'color', 'blue');
set(gcf, 'InvertHardCopy', 'off');
print(gcf,'test1.pdf','-dpdf')
shimizu
  • 998
  • 14
  • 20
  • I forgot to mention, I already tried `InvertHardCopy`. Strange that it works for you, so this might either be a OSX issue (what OS do you use?) or something else related to my installation. – rob Aug 07 '14 at 16:01
  • Strange--I'm on Windows 7. – shimizu Aug 07 '14 at 16:09
  • @rob Maybe try the `export_fig` function in the MATLAB FEX and see if that works? http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig – shimizu Aug 07 '14 at 16:22
  • `export_fig` did not work, BUT there was an interesting error: `Error using hg2.Group/get`. I found out that HG2 was enabled (which is not yet officially supported). Going back to HG1 solved the issue for me – rob Aug 08 '14 at 09:32
1

I'm also using the HG2-Update hack for the sake of beauty. Returning to HG1 is no option for me. Well, I don't have any problems with it, except one: printing directly to .pdf. That's what you're trying to do also. This functionality is still totally screwed up.

The solution: Save with the plot with -dsvg as vector graphic, open the file in Inkscape and save again as pdf with the Export area is drawing checkmark set.

I actually hoped to find a way to script this procedure, without success. So you have to do it manually or wait for the final release of HG2.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • Sounds like an acceptable workaround. By the way, `export_fig` is aimed at supporting HG2. Unfortunately having legends on the figure is breaking it at the moment. I posted an issue here: https://github.com/ojwoodford/export_fig/issues/4 Fixing this would render the workaround unnecessary. – rob Aug 08 '14 at 12:28
  • @rob As I would be the developer of that function I would wait until the final release of HG2. Nobody knows what they will change until then. It's quite easy to write a similar function by yourself, but with full control about everything. And the vector-graphic-export issue one needs to workaround as mentioned. – Robert Seifert Aug 08 '14 at 13:08
  • If you should be able to find a scripted procedure to save figures as pdf with HG2, please let me know :) – rob Aug 09 '14 at 17:22
  • @rob Have a look at my [**Question & Answer here**](http://stackoverflow.com/questions/25229045/exporting-vector-graphics-as-pdf-using-hg2-update-is-not-working-properly/25229055#25229055)! – Robert Seifert Aug 10 '14 at 13:32