MATLAB: Strange behavior on subplots with surf

subplotsurf

Today I noticed a very strange behavior in a rather complex creation of a figure. To see the actual problem, I broke down the code to the essential parts: I create a figure with two subplots in one figure. But whenever I add a surface plot to the second subplot (instead of a normal 2D plot) the properties of the first subplot change. Please run the following code, compare the figures to see the differences:
x=1:20;
y1=randn(20,1);
y2=ones(20,1);
figure;
subplot(2,2,[1,2]);
plot(x,y1,x,y1);hold on;
plot(x,y2,'--b','LineWidth',2); hold off;
subplot(2,2,[3,4]);
surf(x,x,y1*y1','LineStyle','none');
figure;
subplot(2,2,[1,2]);
plot(x,y1,x,y1); hold on;
plot(x,y2,'--b','LineWidth',2); hold off ;
subplot(2,2,[3,4]);
plot(x,y1);
Is there an explantion for this change (especially for the dashed line)? How can I change the properties of the second figure to the unintended and changed properties of figure 1?
I use Matlab R2013b! Thanks for the support!

Best Answer

It appears that surf chooses OpenGL for the renderer (see Figure properties for an explanation of the renderer). Since this is a property of the entire figure, it redraws the first subplot as well. You could add the line
set(gcf,'Renderer','painters')
to restore the default renderer for the first figure.