How to Shade area between 3 curves each with different x-axis values?
6 views (last 30 days)
Show older comments
I have experimental testing results: three curves and the y-axis data for each of the three curves don't follow the same x-axis values.
Looks like this:
I want to to be able to represent this data something like this (crudely done in paint just to show as an example):
I have my data in excel at the moment but plan to make the graphs in Matlab since they'll look better that way, and hoping I can accomplish this too.
I attached the raw data in an excel sheet in case someone wants to fiddle with it.
Thanks!
0 Comments
Accepted Answer
Voss
on 28 Nov 2023
I guess you want to shade the area between the lowest curve and the highest curve for all x.
T = readtable('Book1.xlsx')
% create a common x vector:
x_all = [T.x,T.x_1,T.x_2];
x_min = min(x_all,[],'all');
x_max = max(x_all,[],'all');
x = linspace(x_min,x_max,1000);
% interpolate y, y1, and y2, based on the common x
% (there are some NaNs and/or Infs in some of the ys, so the
% interpolation needs to be done just using the finite values):
idx = isfinite(T.x) & isfinite(T.y);
y = interp1(T.x(idx),T.y(idx),x);
idx = isfinite(T.x_1) & isfinite(T.y_1);
y_1 = interp1(T.x_1(idx),T.y_1(idx),x);
idx = isfinite(T.x_2) & isfinite(T.y_2);
y_2 = interp1(T.x_2(idx),T.y_2(idx),x);
% get the min and max y for each x:
y_min = min([y;y_1;y_2],[],1);
y_max = max([y;y_1;y_2],[],1);
% create a patch to fill the area:
patch(x([1:end end:-1:1]),[y_min, y_max(end:-1:1)],'b','FaceAlpha',0.25,'EdgeColor','none')
% plot the original data lines:
hold on
plot(T.x,T.y,'.-',T.x_1,T.y_1,'.-',T.x_2,T.y_2,'.-')
2 Comments
More Answers (0)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!