Shading of area between two lines not working correctly in MATLAB

Viewed 1864

I am trying to plot the shaded uncertainty (upper and lower bounds) along a line. I tried using the fill function but it is including an area larger than I want. I have an upper error (black), lower error (green), and actual data line (red), like so:

enter image description here

How do I get the area between the green and black lines? I tried the following:

fill([date fliplr(date)], [data_Upper fliplr(data_Lower)], 'r');

But the fill function covers both line areas all the way to the bottom of the plot:

enter image description here

How do I fix this to only shade the area between the lower and upper error line bounds?

2 Answers

I think the problem is that your data is arranged in column vectors (i.e. N-by-1) instead of row vectors (i.e. 1-by-N). Your code above assumes row vectors. Try using flipud and vertical concatenation instead:

fill([date; flipud(date)], [data_Upper; flipud(data_Lower)], 'r');

To elaborate, when you pass an N-by-M matrix to most plotting routines, like fill, it will plot one object per column. In this case, I believe you are creating N-by-2 matrices and passing them to fill, when you should be passing either 1-by-2*N or 2*N-by-1 vectors for a single filled object.

Related