Plot Line Changes When Adding Patches

조회 수: 9 (최근 30일)
Danny
Danny 2011년 6월 19일
Hi, I have a basic plot in MATLAB (plot(x,y)), but when I add patches to the figure (in order to shade regions of night), it changes the original plot line to a step-like line. I am not quite sure why the addition of patches does this. I have two links to images: the first shows the original plot and the second shows what happens to the plot with the addition of patches. Any help would be greatly appreciated. Thank you.

채택된 답변

Patrick Kalita
Patrick Kalita 2011년 6월 20일
When you add transparency to an object in a figure, the figure will automatically change its renderer to OpenGL. You can read more about MATLAB's renderers here and here.
The thing that's messing you up here is that at some point in the rendering pipeline, OpenGL converts floating point numbers to single precision. When you plot things where the x-data is MATLAB date numbers (e.g. creating using datenum), those numbers are large enough that there is a fairly coarse quantization between them in single precision.
One thing you can try doing to work around the problem is to re-scale the plot's x-data into a more "single-precision-friendly" range. The downside would be that you'd then need to set the tick labels manually. Here's a simple example, adapt as necessary for your data:
x = datenum(2011, 6, 20, 1:12, 0, 0);
y = 1:12;
plot(x,y);
set(gcf, 'Renderer', 'openGL') % Looks bad
% Scale x-data
x2 = x - min(x);
x2 = x2./max(x2);
figure;
plot(x2,y);
set(gcf, 'Renderer', 'openGL') % Looks good
% Set ticks manually
set(gca, 'XTick', x2(1:2:end))
set(gca, 'XTickLabel', datestr(x(1:2:end), 'HH:MM'))
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 6월 20일
Interesting observation, Patrick; I never thought about that.
Danny
Danny 2011년 6월 20일
Takes a little working around, but in the end I got the figure I wanted. Thank you so much for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 6월 20일
Your shading involves transparency, and only the OpenGL renderer can handle transparency, so the renderer gets changed to OpenGL for your second plot.
Skipping explanation for the moment: what might help is to give your patches a small negative Z coordinate so they are "below" the lines.
  댓글 수: 1
Danny
Danny 2011년 6월 20일
I gave my patches small negative Z coordinates [-0.25; -0.25; -0.25; -0.25] for each rectangular polygon, but nothing changed. Any Z coordinates smaller or greater than that would make the patches disappear entirely. Maybe there is another step I need to do, but I'm not sure. Thank you for such a quick response earlier and any more help would be great. Thanks again.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by