shade area between graphs
Show older comments
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 Comments
Huijian Huang
on 15 Dec 2017
have you solve this problem at the end? I am also struggling with similar situation
Image Analyst
on 15 Dec 2017
Yes, there were answers below, one of which he accepted though both should work.
Accepted Answer
More Answers (3)
Star Strider
on 28 Feb 2015
Edited: Star Strider
on 28 Feb 2015
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 Comments
Michiel
on 28 Feb 2015
Star Strider
on 28 Feb 2015
I would have to have your data to find out what the problem is. (If you want to attach it using the ‘paperclip’ icon, a .mat file is best.)
Since I don’t have your data, add a NaN value at the end of each of your data arrays (both x and y variables for both data sets) and see if that makes a difference. Just guessing.
Star Strider
on 1 Mar 2015
If you would share your data, I could likely provide a definitive solution. I have no idea why your data are not working with my code.
Star Strider
on 1 Mar 2015
Did my idea work with your transposed vectors? Mine requires row vectors as written here. I perhaps should have specified that.
DETELINA IVANOVA
on 4 Apr 2018
Moved: Adam Danz
on 29 Jan 2024
I am using this last suggestion by Star Strider to plot error bar shading using patch, but in some cases I get in addition to the shading straight lines (see the third plot at the bottom in the attached figure) I have eliminated the points with NaNs. It seems ok in the other two cases (the upper plots in the same figure). What can be causing this?
James Stephenson
on 4 Mar 2021
This answer is too late for you, but maybe it will help others. The last point of your vector is your starting point as well, so the shaded area is doubling back to (0,0). That's why you're getting this wonkiness.
Yash Mistry
on 20 Jul 2021
how can I avoid that?
Star Strider
on 29 Feb 2024
If you have not already done so, check to see if there are NaN values in your data. One way to do that is:
NrNaNs = nnz(isnan(averageFF2W5I))
Your options then are to to remove them using the rmmissing function or interpolate them with the fillmissing function.
However, one problem is that you have column vectors, and you are using fliplr to flip a column vector. That is going to produce the same unchanged column vector, of course.
An improvement to your fill call could be:
fill([averageFF2W5I(:,2); flip(averageFF2W5I(:,2))], [averageFF2W5I(:,8); flip(averageFF2W5I(:,9))], 'g');
Note my use of the semicolon (;) vertical concatenation operator, and the generic flip function that works without further elaboration on vector arguments.
.
Jason Thomas
on 29 Feb 2024
@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
on 29 Feb 2024
My pleasure!
A Vote would be appreciated!
Javier Montalt Tordera
on 12 Apr 2019
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 Comments
SEETHA JAYAN
on 24 Mar 2020
would you please help me ,
i used the above code to shade between min and maximum but I couldnt change edge color and face colur
Javier Montalt Tordera
on 24 Mar 2020
You should be able to change line color and fill color using the properties 'Color' and 'FillColor', respectively, specified as name-value pairs.
Giacomo Giuliani
on 19 May 2022
Is it possible to fill an area between three or more curves without covering none of the curves between the edges?
Image Analyst
on 19 May 2022
@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
on 19 May 2022
Edited: Giacomo Giuliani
on 19 May 2022
It came to my minds one second after I posted. It works! Thanks.
Adam Danz
on 29 Jan 2024
0 votes
Another solution that may be helpful is the fillBetweenAreaCurve function offered by the MATLAB Charting team on the File Exchange.

1 Comment
Les Beckham
on 29 Feb 2024
FYI - it appears that this File Exchange utility requires the Mapping Toolbox.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!




