Fill area between different curves
18 views (last 30 days)
Show older comments
I cannot find a command to fill the area between the maximum and minimum values of these curves:
x1 = [0 1 2.2 4];
y1 = [0.5 2 1.8 1];
x2 = [0 1.2 2 4];
y2 = [0 1 2 1.5];
x3 = [0 1 3 3.5];
y3 = [0 1 2 1.5];
figure
plot(x1,y1);
hold on
plot(x2,y2);
hold on
plot(x3,y3);
I attached an image for better understand my request. Unfortunately, I found a solution just if Y is depending of X (e.g. : https://www.mathworks.com/matlabcentral/answers/180829-shade-area-between-graphs)
2 Comments
Accepted Answer
Star Strider
on 8 Mar 2018
It’s necessary to create common (x,y) values for all the curves with interpolation. Then the solution is straightforward.
If I understand correctly what you want, this works:
x1 = [0 1 2.2 4];
y1 = [0.5 2 1.8 1];
x2 = [0 1.2 2 4];
y2 = [0 1 2 1.5];
x3 = [0 1 3 3.5];
y3 = [0 1 2 1.5];
xm = [x1; x2; x3]; % Create ‘X’ Matrix
xl = [min(xm(:)) max(xm(:))];
xu = linspace(xl(1), xl(2), 1000); % Create Sorted Interpolation Vector
ym = [y1; y2; y3]; % Create ‘Y’ Matrix
for k1 = 1:size(xm,1)
yi(k1,:) = interp1(xm(k1,:), ym(k1,:), xu, 'linear','extrap'); % Interpolate To Provide Common (x,y) Values
end
ymin = min(yi);
ymax = max(yi);
figure
patch([xu(:)' fliplr(xu(:)')], [ymin fliplr(ymax)], [0.1 0.9 0.1], 'FaceAlpha',0.3) % Plot Filled Background
hold on
plot(x1,y1)
plot(x2,y2)
plot(x3,y3)
10 Comments
Star Strider
on 13 Mar 2018
There’s no one specific command. You would first need to use the size function to determine the vector sizes. Then compare the row size (dimension 1, so size(data,1) if ‘data’ is your array name), then compare them.
One way to do the initial check is:
sz1 = 200; % Row Size Of First Data Set
sz2 = 201; % Row Size Of Second Data Set
sz3 = 211; % Row Size Of Third Data Set
sz_eq = isequal(sz1, sz2, sz3); % If All Are Equal=1, Not Equal = 0
min_rows = min([sz1 sz2 sz3]); % Minimum Row Size
max_rows = max([sz1 sz2 sz3]); % Maximum Row Size
You can of course define other criteria. Then trim your data matrices as you wish, the only constraint being that the ‘trimmed’ row size is less than ‘min_rows’.
More Answers (0)
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!