1

I am creating surface plots with a transparent figure background in Matlab R2015b. Consider the code

n=49; 
h=figure;
[x,y]=meshgrid(1:n,1:n);
surf(x,y,peaks(n),'EdgeColor','none')
set(h,'Color','none')
set(h, 'InvertHardCopy', 'off');
print(h,'-dpdf','peaks.pdf')

which gives me a pdf file peaks.pdf where the axes background is white, but the figure background transparent.
However, if I set n=1000, the background is no longer transparent, but black (as the figure is displayed in Matlab).
Does anyone know, what the issue is, and how to prevent this? Thanks!

cauchy42
  • 191
  • 2
  • 9
  • Probably related to OpenGL rendering. See [https://www.mathworks.com/matlabcentral/answers/24434-opengl-and-print](https://www.mathworks.com/matlabcentral/answers/24434-opengl-and-print). And other related google search results. – Rotem Sep 12 '16 at 17:11

1 Answers1

1

The key is the property set(h, 'InvertHardCopy', 'off');

From the MATLAB help:

InvertHardcopy — Figure background color when printing or saving
'on'(default) | 'off'
Figure background color when saving or printing, specified as one of these values:

'on' — Change the figure background and axes background colors to white.
'off' — Use the same colors as the colors on the display. To change the figure background color on the display, use the Color property of the figure. To change the axes background color, use the Color property of the axes.

Accordingly, you should use set(h, 'InvertHardCopy', 'on'); to get the desired pdf output.

Martin S
  • 814
  • 2
  • 11
  • 24
  • Setting `InvertHardCopy` to `on` would change the background colour to white, as you stated, but I am interested in having a transparent background for the figure instead. So I actually need to set it to `off`. – cauchy42 Sep 13 '16 at 06:55
  • Sorry, I missed that part. I think setting the renderer with `print(h,'-painters','-dpdf','peaks.pdf')` will produce the output with transparent background. At least it did the trick on my system (Win7, MATLAB R2016a) with `n=100`. However, setting `n=1000` will be really slow with this renderer. I just interrupted the script after approx. 90 minutes.... – Martin S Sep 13 '16 at 08:44