2
votes

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!

2

2 Answers

3
votes

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

4
votes

I guess that you create the fill with

fill([xData1;xData2],[yData1;yData2])

where xData1 is a n-by-1 array of x-data for your first curve. This will lead to a weirdly-shaped polygon because the 'corners' of the polygon are not properly ordered.

Instead, you should do

fill([xData1;xData2(end:-1:1)],[yData1;yData2(end:-1:1])

i.e. flip the order of one of the two data sets.