3
votes

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
With this data: date = 1:5; data_Upper = date; data_Lower = date-1;, everything seems to work. Can you a give a minimal reproducible example?David
Does this help?Luis Mendo

2 Answers

1
votes

It seems to me that you have used wrong lower bounds data.

Here is a simple MATLAB example for that, you could modify it to include your lines,

x =[1 2 3 4 5];%Both lines share same x value
y1=x+1;%Equation for first line
y2=2*x;%Equation for second line
% plot the line edges
hold on 
plot(x, y1, 'LineWidth', 1);
plot(x, y2, 'LineWidth', 1);
% plot the shaded area
fill([x fliplr(x)], [y2 fliplr(y1)], 'r');

The outcome of running this is

enter image description here

Good luck!

0
votes

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.