matlab draw curves with shadows

% 示例数据
x = linspace(0,10,200);
y = sin(x);
stdv = 0.2 + 0.1*abs(cos(0.5*x)); % 假设的 std
% 构造上下边界
y1 = y + stdv;
y2 = y - stdv;
figure; hold on;
% 画阴影带(注意 x 顺序拼接)
X = [x, fliplr(x)];
Y = [y1, fliplr(y2)];
hPatch = fill(X, Y, [0.7 0.85 1]); % 浅蓝色
set(hPatch, 'EdgeColor', 'none', 'FaceAlpha', 0.4); % 半透明且没有边线
% 再画中间曲线
plot(x, y, 'b-', 'LineWidth', 2);
xlabel('x'); ylabel('y'); title('Mean ± std band');
 
x = linspace(0,6*pi,400);
y = sin(x).*exp(-0.05*x);
figure; hold on;
hArea = area(x, y);
hArea.FaceColor = [0.8 0.8 0.9];
hArea.EdgeColor = 'none';
hArea.FaceAlpha = 0.45;
plot(x, y, 'k', 'LineWidth', 1.5);
 
 
x = linspace(0,10,500);
y = cos(x) + 0.2*sin(3*x);
figure; hold on;
% shadow (偏移量)
dx = 0.06; dy = -0.06;
plot(x+dx, y+dy, '-', 'Color', [0 0 0 0.25], 'LineWidth', 6); % 需要 R2019b 以上支持 RGBA
% 若你的 MATLAB 不支持 RGBA 直接在 plot,可用灰色粗线并降低 alpha(通过 patch 模拟)
plot(x, y, 'r-', 'LineWidth', 2);
 
 
figure; hold on;
numShades = 8;
for k = numShades:-1:1
a = 0.06 * (k/numShades); % 偏移量
alphaVal = 0.04 + 0.12*(k/numShades);
plot(x + a, y - a, 'Color', [0 0 0 alphaVal], 'LineWidth', 2 + k*0.2);
end
plot(x, y, 'r', 'LineWidth', 2.5);
 
 
x = linspace(0,6,200);
ys = [sin(x); sin(x+0.5); sin(x-0.6)]; % 三条示意曲线
stds = 0.12 * (1 + 0.5*rand(3,1)); % 各自的 std 规模
colors = lines(3);
figure; hold on;
for i = 1:3
y = ys(i,:);
s = stds(i);
% band
X = [x, fliplr(x)];
Y = [y+s, fliplr(y-s)];
p = fill(X, Y, colors(i,:), 'EdgeColor', 'none');
p.FaceAlpha = 0.25;
plot(x, y, 'Color', colors(i,:), 'LineWidth', 1.8);
end
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2025-08-10 21:04  WaiHoYu  阅读(19)  评论(0)    收藏  举报