Plot deviation of curve as shaded area inside

조회 수: 33 (최근 30일)
Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 22일
댓글: Sokratis Panagiotidis 2022년 7월 23일
Hi,
A common question here in this forum is how to plot an area between two curves.
Here's one type of a common answer:
x = transpose(1:100);
y = x.^2;
curve1 = y*1.05;
curve2 = y*0.95;
x2 = [x flipud(x)];
inBetween = [curve1, curve2];
fill(x2, inBetween, 'k', 'LineStyle', 'none');
hold on;
plot(x, y, 'r', 'LineWidth', 2);
This is the result I get:
I used
alpha(.1)
to make the shaded area transparent.
What I would like to have is the area between those two curves shaded, since I want to show the red curve's deviation without having to use the green lines. Hence why I removed the Linestyle. But I get something like a linear fill between the starting and endpoint of the vectors.
How can I do that without too many lines of code? I'm afraid my laptop is at a certain limit with the actual length of vectors I have to plot (>800.000 values).
Also if you know if and how it's possible to alternate the shaded area like a zebra (white-black-white-black, etc) I'd appreciate if you tell me that!
Thank you!

채택된 답변

Star Strider
Star Strider 2022년 7월 22일
I generally prefer patch to fill
x = transpose(1:100);
y = x.^2;
curve1 = y*1.05;
curve2 = y*0.95;
x2 = [x flipud(x)];
inBetween = [curve1 curve2];
patch([x; flip(x)], [inBetween(:,1); flip(inBetween(:,2))], 'k', 'LineStyle', 'none', 'FaceAlpha',0.25);
hold on;
plot(x, y, 'r', 'LineWidth', 2);
The approach to writing patch calls is to provide a defined area that the function can fill. Here, that is provided by plotting ‘curve1’ against ‘x’ and then plotting ‘flip(curve2)’ against ‘flip(x)’ completing the region to be filled.
.
  댓글 수: 2
Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 22일
Hey man thanks a lot!
This is what I was looking for.
Star Strider
Star Strider 2022년 7월 22일
As always, my pleasure!

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

추가 답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2022년 7월 22일
편집: Bjorn Gustavsson 2022년 7월 22일
Have a look at the file exchange. There there are multiple "shaded-error-plots". For example (without ordering of merit):
I have used a majority of these functions at different times to god result, now I have no memory of why one or the other was preferable.
HTH
  댓글 수: 3
Bjorn Gustavsson
Bjorn Gustavsson 2022년 7월 22일
In what ways were those functions unsuitable for your data?
Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 23일
This is what I get when using errorfill:
Unable to perform assignment because the indices on the left side are not compatible with the
size of the right side.
Error in errorfill (line 111)
E(:,n)= [y+temp(1,:) y(end:-1:1)-temp(2,end:-1:1)];
Error in Errorfill_Test (line 7)
errorfill(x, y, E3, '.');
My vector has almost 600 thousand entries so it's hard to tell what the problem is.

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

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by