Bound Fill Between Plot Lines

6 views (last 30 days)
I'm trying to fill between two lines on a plot to create a confidence interval plot around the original data plot. The upper and lower bounds of y have been calculated and exist in 25x1 vectors along with the x and y data. The basic code I have (included below) so far works to an extent, as in it does fill between the lines, however it connects one end of the plot back to the start. I imagine that the data needs bounding in some way as to prevent this, but I am at a complete loss of how to do this. Any help would be greatly appreciated. Thanks.
x_plot = [x_data, fliplr(x_data)];
y_plot = [lower_confidence_band_y, fliplr(upper_confidence_band_y)];
hold on
plot(x, y_data);
fill(x_plot, y_plot);
hold off
  1 Comment
Big Bassist 97
Big Bassist 97 on 4 Jan 2018
Thanks for the reply's. Here's the data...
x_data=[0.268181818181818 1.51695906432749 2.49793814432989 3.49232736572890 4.42794117647059 5.41769539078156 6.43429319371728 7.39283746556474 8.55216152019004 9.44566210045661 10.4379537953795 11.4717299578059 12.4268401486989 13.3968992248062 14.4111111111111 15.4357664233577 16.4672000000000 17.3859375000000 18.4820512820513 19.4794117647059 20.3962962962963 21.3090909090909 22.3928571428571 23.4750000000000 24.5200000000000]'
y_data = [0.0232727272727273 0.0705847953216374 0.654948453608247 4.07634271099744 10.8494852941176 28.1094789579158 48.4079319371727 75.0726721763085 117.936555819477 160.226118721461 203.138613861386 243.557257383966 263.215687732343 266.310077519380 272.539145299145 272.562043795620 272.125840000000 272.562500000000 270.974358974359 270.882352941176 269.185185185185 265.454545454545 263.642857142857 259 258.200000000000]'
lower_confidence_band_y = [-0.00661885353585373 -0.00536025923416390 0.259036768443956 3.20670979581549 9.98917666115861 26.8996259684148 46.2634649231698 72.0214271939945 114.659389514261 156.776694937094 198.666208420111 239.727204924423 260.676209965518 262.237289670594 272.095569696281 272.203440643073 270.856462458917 272.104935554064 269.912526954261 269.820171389653 267.899495198740 263.997128153926 260.300689913255 256.228192351301 256.299744414177]'
upper_confidence_band_y = [0.0531643080813083 0.146529849877439 1.05086013877254 4.94597562617939 11.7097939270767 29.3193319474169 50.5523989511757 78.1239171586225 121.213722124694 163.675542505828 207.611019302661 247.387309843509 265.755165499167 270.382865368166 272.982720902010 272.920646948168 273.395217541083 273.020064445936 272.036190994457 271.944534492700 270.470875171631 266.911962755165 266.985024372460 261.771807648699 260.100255585823]'
Many Thanks

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Jan 2018
Edited: Star Strider on 4 Jan 2018
‘it connects one end of the plot back to the start’
You must have a duplicated data value at the beginning or end of whatever part of the plot is causing the problem. The following code works for me without the error you reported:
x_data = linspace(0, 10, 25);
y_data = 5 + 2*x_data + 0.01*randn(size(x_data));
lower_confidence_band_y = y_data - 2;
upper_confidence_band_y = y_data + 2;
x_plot = [x_data, fliplr(x_data)];
y_plot = [lower_confidence_band_y, fliplr(upper_confidence_band_y)];
figure(1)
plot(x_data, y_data)
hold on
fill(x_plot, y_plot, 'g', 'FaceAlpha',0.5)
hold off
EDIT Added code with solution.
I would keep them all as column vectors, then concatenate them in ‘x_plot’ and ‘y_plot’. (Both versions of those vectors produce the same result.)
This Works
x_plot = [x_data; flipud(x_data)]; % Original Column Vectors
y_plot = [lower_confidence_band_y; flipud(upper_confidence_band_y)]; % Original Column Vectors
x_plot = [x_data' fliplr(x_data')]; % Transposed To Row Vectors
y_plot = [lower_confidence_band_y' fliplr(upper_confidence_band_y')]; % Transposed To Row Vectors
figure(1)
plot(x_data, y_data)
hold on
patch(x_plot, y_plot, 'g', 'FaceAlpha',0.3, 'EdgeColor','g', 'EdgeAlpha',0.8)
hold off
I added some tweaks to the patch call to make the plot a little easier to see. Experiment to get the result you want.
  2 Comments
Big Bassist 97
Big Bassist 97 on 4 Jan 2018
Thankyou! This works great, Very much appreciated.
Star Strider
Star Strider on 4 Jan 2018
As always, my pleasure!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 4 Jan 2018
Maybe try patch() instead of area().

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!