3

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?

gnovice
  • 125,304
  • 15
  • 256
  • 359
user3052817
  • 245
  • 3
  • 9

2 Answers2

1

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!

1

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.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Nowadays, `fliplr` and `flipud` are deprecated, in favor of just plain `flip`. Using that function it is less likely that the flip direction is wrong. – Cris Luengo Aug 29 '23 at 17:31