shade area between graphs

3,674 ビュー (過去 30 日間)
Michiel 2015 年 2 月 28 日
コメント済み: Image Analyst 2022 年 6 月 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 件のコメント
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');
  11 件のコメント
Image Analyst
Image Analyst 2022 年 6 月 17 日
@MadjeKoe simply don't call plot() and only call 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);
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.
  6 件のコメント
Yash Mistry
Yash Mistry 2021 年 7 月 20 日
how can I avoid that?


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 件のコメント
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.


DETELINA IVANOVA 2018 年 4 月 4 日
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?


Find more on Graphics Performance 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!

Translated by