1

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?

4
  • When I copied your code into Matlab it generated a plot similar to the first one.
    – Calvin
    Jul 28, 2015 at 21:50
  • @CalvinWhealton I'm getting the same issue. It seems like whenever the area(x,y+i,i) command is called after the first one, the level of the previous one gets changed to the new one. Jul 29, 2015 at 1:40
  • Just out of curiosity, what version are you using? Jul 29, 2015 at 1:41
  • @CalvinWhealton, ok on my Matlab 2014a it is working the way I expect it. I must check what the other version of Matlab was that I used. I think that it was actually 2014b, but I will go and have a look later! Jul 29, 2015 at 7:42

1 Answer 1

2

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.

1
  • Thanks for the solution @bla. But you've already pointed out what makes things more complicated: The traces do not always end on the same value and hence that needs to be taken into account. That's why I wanted to use area, as the function should supply that. Anyway, in 2014a it is working as expected and I have a result now, even though the answer why it was not working is not found, yet. Thanks for linking to areashade. It looks promising! Jul 29, 2015 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.