Strange behavior on subplots with surf
6 views (last 30 days)
Show older comments
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!
2 Comments
dpb
on 19 Feb 2015
Edited: dpb
on 20 Feb 2015
No klew...it acts the same way in R2012b(*), btw, so it's not the new graphics engine.
For some reason the lines are rendered differently despite style and width parameters being the same. I went side-by-side and retrieved the handles to the two lines and set the properties manually to a range of line widths and the rendering still isn't the same between the two. I could observe no properties that were different that are visible.
BTW, why use
subplot(2,2,[1,2]);
instead of the syntactically simpler
subplot(2,1,1);
for full-width, two-high suplots??
ADDENDUM
(*) Just out of curiosity, I tried the earliest version I have installed (R11). The difference isn't quite as noticeable but they're still different. With it the apparent width is almost indistinguishable between the two but there's a notable difference in the length of the dashed line segments between the two.
Really, really, bizzaro!!! Be interesting to hear what TMW has to say on this one...
Accepted Answer
Andrew Newell
on 20 Feb 2015
Edited: Andrew Newell
on 20 Feb 2015
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.
3 Comments
dpb
on 20 Feb 2015
Sure, you can use renderer of choice...
set(gcf,'renderer','OpenGL')
I hadn't thought of the renderer itself, Sven, good catch. I shoulda' thunk of it as a likely culprit when the styles changed.
More Answers (0)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!