Shading of area between two lines not working correctly in MATLAB

北战南征 提交于 2019-12-31 01:56:10

问题


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:

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:

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


回答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

Good luck!




回答2:


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.



来源:https://stackoverflow.com/questions/46245958/shading-of-area-between-two-lines-not-working-correctly-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!