shade area between graphs

조회 수: 1,497 (최근 30일)
Michiel
Michiel 2015년 2월 28일
댓글: William Rose 2024년 4월 17일
I want to shade the area between these graphs
but this certainly does not do the trick:
fill([t t(end:-1:1)],[y1 y2(end:-1:1)],'y')
I also downloaded and tried "jblill"
but that did not seem to amend the outcome so far
  댓글 수: 2
Huijian Huang
Huijian Huang 2017년 12월 15일
have you solve this problem at the end? I am also struggling with similar situation
Image Analyst
Image Analyst 2017년 12월 15일
Yes, there were answers below, one of which he accepted though both should work.

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

채택된 답변

Image Analyst
Image Analyst 2015년 2월 28일
Try using fill() instead of area():
x = 1 : 300;
curve1 = log(x);
curve2 = 2*log(x);
plot(x, curve1, 'r', 'LineWidth', 2);
hold on;
plot(x, curve2, 'b', 'LineWidth', 2);
x2 = [x, fliplr(x)];
inBetween = [curve1, fliplr(curve2)];
fill(x2, inBetween, 'g');
  댓글 수: 13
Jason Thomas
Jason Thomas 2024년 2월 29일
Hoping this post from 9 years ago is still monitored.
I tried the suggestion above, but instead of filling the area green, it just added thin black lines at the upper and lower bounds. What am I missing?
plot(averageFF2W5I(:,2),averageFF2W5I(:,1), 'k', 'LineWidth', 2);
plot(averageFF2W5I(:,2),averageFF2W5I(:,[8 9]), 'k--', 'LineWidth', 2);
fill([averageFF2W5I(:,2), fliplr(averageFF2W5I(:,2))], [averageFF2W5I(:,8), fliplr(averageFF2W5I(:,9))], 'g');
averageFF2W5I(:,2) = Strain Values
averageFF2W5I(:,1) = Stress Sample Mean
averageFF2W5I(:,[8 9]) = 95% Confidence Interval
William Rose
William Rose 2024년 4월 17일
I don't have your array, so I will use analogous data.
x=0:20; y=x.^.5; ci1=0.8*y; ci2=1.2*y;
fill([x, fliplr(x)], [ci1, fliplr(ci2)], 'g', EdgeColor='none');
hold on
plot(x,y, 'k', 'LineWidth', 2)
plot(x,[ci1;ci2], 'k--', 'LineWidth', 2)
I moved fill() before plot(), so that the line in the middle would not get covered by the fill. Alternatively, you could specify a semitransparent fill. I specified no edge for the fill. I added hold on after fill().

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

추가 답변 (3개)

Star Strider
Star Strider 2015년 2월 28일
편집: Star Strider 2015년 2월 28일
Try this:
x = linspace(0,10*pi);
y1 = sin(x);
y2 = cos(x);
figure(1)
plot(x, y1)
hold on
plot(x, y2)
patch([x fliplr(x)], [y1 fliplr(y2)], 'g')
hold off
Experiment with your data to get the result you want.
  댓글 수: 10
Jason Thomas
Jason Thomas 2024년 2월 29일
@Star Strider THANK YOU!
There were several NaN values in the confidence interval. Once I removed those with rmmissing and used your code with "flip" instead of "fliplr" it worked perfectly.
Star Strider
Star Strider 2024년 2월 29일
My pleasure!
A Vote would be appreciated!

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


Javier Montalt Tordera
Javier Montalt Tordera 2019년 4월 12일
This can be simplified using the function shade (in MATLAB File Exchange).
The syntax for the above problem would be:
shade(t,y1,t,y2,'FillType',[1 2;2 1]);
The FillType option specifies that the area between lines 1 and 2 should be filled, whether 1 is above 2 or the other way round.
  댓글 수: 5
Image Analyst
Image Analyst 2022년 5월 19일
@Giacomo Giuliani, yes though to not cover the line plots you might plot (or re-plot) the line plots after you've plotted the patches between the curves.
Giacomo Giuliani
Giacomo Giuliani 2022년 5월 19일
편집: Giacomo Giuliani 2022년 5월 19일
It came to my minds one second after I posted. It works! Thanks.

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


Adam Danz
Adam Danz 2024년 1월 29일
Another solution that may be helpful is the fillBetweenAreaCurve function offered by the MATLAB Charting team on the File Exchange.
  댓글 수: 1
Les Beckham
Les Beckham 2024년 2월 29일
FYI - it appears that this File Exchange utility requires the Mapping Toolbox.

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

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by