How to Shade area between 3 curves each with different x-axis values?
7 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1554792/image.png)
I want to to be able to represent this data something like this (crudely done in paint just to show as an example):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1554797/image.png)
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 个评论
采纳的回答
Voss
2023-11-28
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,'.-')
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!