TheodorBecker
TheodorBecker

Reputation: 249

Coloring plot below curve in Matlab

I would like to draw various curves in the same figure, and color the area between the respective curve and a given basevalue.

Here is a toy example of code that I hoped was working, but for some reason the function area does not work the way I would expect:

x = 0:1/30:30;
y = sin(x);

figure
hold on
for i = 1:3:10
    area(x,y+i,i)
end

What I would expect and need is something like this: enter image description here

However, what Matlab is plotting is this: enter image description here

Is it just too late and I should go to bed or what is the problem here?

Upvotes: 1

Views: 230

Answers (1)

bla
bla

Reputation: 26069

just use fill or patch instead of area :

x = linspace(0,10*pi)
y = sin(x);

figure
hold on
for i = 1:3:10
    patch(x,y+i,i);
end

enter image description here

note that I changed the x limit so y will end on the same value it started with to get the crossed vertical line. For a more generic treatment look here.

Upvotes: 2

Related Questions