This is not properly an answer, as it doesn't exactly explain why this is happening, but it provides a workaround, along with some more observations of the weird behaviour.
Scope:
I tried your example and indeed:
- pre HG2 (R2013a): Same behavior than you described
- HG2 (R2015a) : No problem, everything is there.
Workaround:
After a few trial and error, I worked out that it is a specific behaviour of the painter
renderer in pre HG2 versions.
If you change the renderer to any other than the default painter
, you get back your image and your superimposed lines.
set(gcf,'Renderer','zbuffer')
%// OR
set(gcf,'Renderer','opengl')
Observations:
Note that I also tried to:
- display the lines first (no problem), then the image (and reorder using
uistack
) => same black image.
- use multiple axes => black frame

And to show you how persistent is the glitch:
- if you delete all the lines, the image does not reappear (=black frame).
- if you delete all graphics objects, then re display the image => black frame
- if you
cla
or even clf
then re display the image => black frame
The only way I found to get the image displayed is to change the renderer as described above.
Printing/Saving
Initially, I thought the change of renderer was happening behind the scene when you were saving the figure, thereby allowing the final output to be fully displayed. Unfortunately, by exploring a bit more it doesn't seem to be so simple.
I tried different version with print
(instead of saveas
) since it allows you to select the renderer. For each renderer I chose 2 formats, PDF
which uses the ghostscript
engine, and PNG
which uses the Matlab engine:
%%
print(1,'-dpng','-painters','testimageP.png')
print(1,'-dpng','-zbuffer' ,'testimageZ.png')
print(1,'-dpng','-opengl' ,'testimageO.png')
%%
print(1,'-dpdf','-painters','testimageP.pdf')
print(1,'-dpdf','-zbuffer' ,'testimageZ.pdf')
print(1,'-dpdf','-opengl' ,'testimageO.pdf')
Well, after results I am still unsure of what is happening. All these saved figures show the proper image and the lines on top... But:
The 3x png
images (Matlab
engine) are exactly similar. They do not even show slight difference in saturation like you can observe when you switch the renderer manually. This made me think that Matlab chose to ignore my renderer specification. It just decided which one was the most relevant and went ahead printing 3 times the same figure. So I thought may be the painter
renderer wasn't used and that's why the images were shown.
Well not so fast. On the 3x pdf
images (ghostscript
engine) ... I can observe the small nuances between the 3 pictures ... so the renderer wasn't the same between them. The painter
was used on one of them, and successfully rendered the image.
So in conclusion, it seems the painter
renderer is only glitchy when applied to a (pre-HG2) figure!