Search code examples
matlabplottime-seriesfill

Two time series plots and shading between them...MATLAB


I am using MATLAB to plot two lines of a time series... (a min and max line)

I have the points converging at a single point at the end of the data.

I am trying to fill the area in between the lines and then plot other lines on top of the shaded area.

Here is my problem:

When I use "fill" it does exactly what I want it to do...but it draws a line from the last point of the data back to the initial data point. How do I get rid of it?

Here is a very vague sketch of my 2 examples:

image

image2

The line below the graph is what I am talking about...

Any ideas how to avoid that?

Thanks!


Solution

  • As @Jonas explained (beat me to it), you need to properly order the data of the two time-series. Let me add an example to that:

    %# first series
    x1 = linspace(pi/4, 5*pi/4, 100);
    y1 = cos(x1);
    
    %# second series
    x2 = linspace(pi/4, 5*pi/4, 100);
    y2 = sin(x2);
    
    subplot(121), fill([x1 x2], [y1 y2], 'r')
    subplot(122), fill([x1 fliplr(x2)], [y1 fliplr(y2)], 'r')
    hold on
    plot(x1,y1, 'Color','b', 'LineWidth',3)
    plot(x2,y2, 'Color','g', 'LineWidth',3)
    

    enter image description here