MATLAB Answers

shade area between graphs

조회 수: 3,840(최근 30일)
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 15 Dec 2017
have you solve this problem at the end? I am also struggling with similar situation
Image Analyst
Image Analyst 15 Dec 2017
Yes, there were answers below, one of which he accepted though both should work.

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

채택된 답변

Image Analyst
Image Analyst 28 Feb 2015
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');

  댓글 수: 7

표시 이전 댓글 수: 4
Hassan Raheem
Hassan Raheem 31 Aug 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 17 Apr 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 1 Feb 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?

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

추가 답변(3개)

Star Strider
Star Strider 28 Feb 2015
편집: Star Strider 28 Feb 2015
Try this:
x = linspace(0,10*pi);
y1 = sin(x);
y2 = cos(x);
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.

  댓글 수: 4

표시 이전 댓글 수: 1
Star Strider
Star Strider 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
Star Strider 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
Star Strider 1 Mar 2015
Did my idea work with your transposed vectors? Mine requires row vectors as written here. I perhaps should have specified that.

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

Javier Montalt Tordera
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.

  댓글 수: 2

SEETHA JAYAN 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
You should be able to change line color and fill color using the properties 'Color' and 'FillColor', respectively, specified as name-value pairs.

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

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?

  댓글 수: 0

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

Community Treasure Hunt

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

Start Hunting!

Translated by