fill problem--shading the wrong parts of a curve

17 views (last 30 days)
I'm having trouble using fill to shade the area between two curves. The code I'm using is
fill(x,[y1 y2],'k')
I've also tried
fill([x x],[y1 y2],'k')
which gives the same result. Is there something obvious I'm missing? Below is the plot I'm getting. I'd like to shade the are between the two curves, not between the first and last y value of each curve. Ideas?

Accepted Answer

Sean de Wolski
Sean de Wolski on 11 Sep 2012
And there is a problem we all run into the first time we use fill() :)
x = 1:10;
y1 = rand(1,10)+1;
y2 = rand(1,10)-0.5;
fill([x fliplr(x)],[y1 fliplr(y2)],'c')
The sorting order is important. You want to trace the outside as if you were to walk around your x/y coordinates.
  1 Comment
Chad Greene
Chad Greene on 11 Sep 2012
Brilliant! The "walking the perimeter" explanation is exactly what I was missing.
For my columns of data I had to make a little tweak, but that was easy now that I understand how it works.
Thank you for your help!

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!