shade area between graphs

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 comentarios

Huijian Huang
Huijian Huang el 15 de Dic. de 2017
have you solve this problem at the end? I am also struggling with similar situation
Image Analyst
Image Analyst el 15 de Dic. de 2017
Yes, there were answers below, one of which he accepted though both should work.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 28 de Feb. de 2015

41 votos

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 comentarios

Michiel
Michiel el 1 de Mzo. de 2015
gives the exact same plot as I posted above (although I used the graph from my original post this time, which is arbitrary)
Image Analyst
Image Analyst el 1 de Mzo. de 2015
Even if I make data that cris-crosses, it seems to work:
% Create sample data.
x = 1 : 100;
curve1 = log(x);
curve2 = log(x) + rand(1, length(x)) - 0.5;
% Plot it.
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');
grid on;
Michiel, please attach a few of your data files so we can do it with your actual data.
Michiel
Michiel el 1 de Mzo. de 2015
I found the problem... I used a column vector instead of a row vector... had to transpose the damn thing... rookie mistake 8)
Image Analyst
Image Analyst el 1 de Mzo. de 2015
Editada: Image Analyst el 1 de Mzo. de 2015
OK. Good, so fill() worked. Can you mark my Answer as Accepted then? By the way, for column vectors, use semicolon instead of comma and flipud() instead of fliplr():
% Create sample data as column vectors.
x = [1 : 100]';
curve1 = log(x);
curve2 = log(x) + rand(length(x), 1) - 0.5;
% Plot it.
plot(x, curve1, 'r', 'LineWidth', 2);
hold on;
plot(x, curve2, 'b', 'LineWidth', 2);
% For column vectors, use flipud(), for row vectors use fliplr().
x2 = [x; flipud(x)]; % Use ; instead of ,
inBetween = [curve1; flipud(curve2)]; % Use ; instead of ,
fill(x2, inBetween, 'g');
grid on;
Hassan Raheem
Hassan Raheem el 31 de Ag. de 2017
Hi, if I have to curves , but the data for each curve differ from the another (the length is not the same) , How can I plot them? Thank you EX: x1 = read from csv file1 first column x2= read from csv file2 first column y1= read from csv file1 2nd col. y2= read from csv file2 2nd col plot(x1,y1) hold on plot (x2,y2) Then how can I shade the area between these two curves?
Julien Reynes
Julien Reynes el 17 de Abr. de 2018
It is exactly what a wanna do, but my problem is that my two curves contain NaN (they are not continuous) So if I plot, no filling appears, just boundaries. Any idea how to solve it easily?
Tom
Tom el 1 de Feb. de 2019
If you have data sets of different lengths in the same figure, I suggest interpolating both datasets in such a way that their x-axes are identical. Since the PLOT function implicitly interpolates the data linearly, this will not change the way your data is displayed.
If you have data that contains NaNs, what do you want to achieve? How should the filling be? Should it stop at discontinuities? Should it act as if the discontinuities aren't there?
Tilen Thaler
Tilen Thaler el 16 de Feb. de 2022
Thank you. Exactly what I was looking for.
Roger Vegeta
Roger Vegeta el 18 de Abr. de 2022
Does this method work when both curves have different array lenght?
MadjeKoe
MadjeKoe el 17 de Jun. de 2022
Does anybody maybe know how to remove the outside lines and to only plot the shade? Thank you in advance
Image Analyst
Image Analyst el 17 de Jun. de 2022
@MadjeKoe simply don't call plot() and only call fill().
Jason Thomas
Jason Thomas el 29 de Feb. de 2024
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
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().

Iniciar sesión para comentar.

Más respuestas (3)

Star Strider
Star Strider el 28 de Feb. de 2015
Editada: Star Strider el 28 de Feb. de 2015

13 votos

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 comentarios

Michiel
Michiel el 28 de Feb. de 2015
ttom = time vector [1:variable_interval:20]
y1 = the red line
y2 = the blue line
patch([ttom fliplr(ttom)], [y1 fliplr(y2)], 'g')
I had this before as well, somehow I'm doing it wrong
Star Strider
Star Strider el 28 de Feb. de 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
Star Strider el 1 de Mzo. de 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
Star Strider el 1 de Mzo. de 2015
Did my idea work with your transposed vectors? Mine requires row vectors as written here. I perhaps should have specified that.
DETELINA IVANOVA
DETELINA IVANOVA el 4 de Abr. de 2018
Movida: Adam Danz el 29 de En. de 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
James Stephenson el 4 de Mzo. de 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
Yash Mistry el 20 de Jul. de 2021
how can I avoid that?
Star Strider
Star Strider el 29 de Feb. de 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
Jason Thomas el 29 de Feb. de 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
Star Strider el 29 de Feb. de 2024
My pleasure!
A Vote would be appreciated!

Iniciar sesión para comentar.

Javier Montalt Tordera
Javier Montalt Tordera el 12 de Abr. de 2019

9 votos

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 comentarios

SEETHA JAYAN
SEETHA JAYAN el 24 de Mzo. de 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
Javier Montalt Tordera el 24 de Mzo. de 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
Giacomo Giuliani el 19 de Mayo de 2022
Is it possible to fill an area between three or more curves without covering none of the curves between the edges?
Image Analyst
Image Analyst el 19 de Mayo de 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
Giacomo Giuliani el 19 de Mayo de 2022
Editada: Giacomo Giuliani el 19 de Mayo de 2022
It came to my minds one second after I posted. It works! Thanks.

Iniciar sesión para comentar.

Adam Danz
Adam Danz el 29 de En. de 2024

0 votos

Another solution that may be helpful is the fillBetweenAreaCurve function offered by the MATLAB Charting team on the File Exchange.

1 comentario

Les Beckham
Les Beckham el 29 de Feb. de 2024
FYI - it appears that this File Exchange utility requires the Mapping Toolbox.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Feb. de 2015

Comentada:

el 17 de Abr. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by