方法一
正常绘制图形,然后使用 patch 手动绘制误差线。补丁的数据(坐标和颜色)取自绘制的图表,补丁的 alpha 可以设置为任何所需的值。
clear all
close all
clc
error_alpha = .4;
error_width_factor = .01;
x = 0:pi/10:pi;
y = sin(x);
y2 = cos(x);
y3 = atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
ee = [e; e2; e3];
figure
hold on
hp(1) = plot(x,y);
hp(2) = plot(x,y2);
hp(3) = plot(x,y3);
w = diff(xlim)*error_width_factor;
for m = 1:numel(hp)
for n = 1:numel(hp(m).XData)
patch(hp(m).XData(n)+[-1 1 1 -1]*w, hp(m).YData(n)+[-1 -1 1 1]*ee(m,n), 'w',...
'FaceColor', hp(m).Color, 'FaceAlpha', error_alpha, 'EdgeColor', 'none');
end
end
方法二
与以前类似,但使用更窄的补丁并使用与图形相关的水平移位来绘制它们(如@Patrik 所建议的那样)。应用 alpha 值有助于保持图形更轻。
该代码是方法 1 的修改版本。此处显示的示例包含 101 个数据值,并且仍然相当可见。
clear all
close all
clc
error_alpha = .4;
error_width_factor = .003;
x = 0:pi/50:pi;
y = sin(x);
y2 = cos(x);
y3 = atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
ee = [e; e2; e3];
figure
hold on
hp(1) = plot(x,y);
hp(2) = plot(x,y2);
hp(3) = plot(x,y3);
w = diff(xlim)*error_width_factor;
m0 = (numel(hp)+1)/2;
for m = 1:numel(hp)
for n = 1:numel(hp(m).XData)
patch(hp(m).XData(n)+[-1 1 1 -1]*w+w*(m-m0),...
hp(m).YData(n)+[-1 -1 1 1]*ee(m,n), 'w', 'FaceColor', hp(m).Color, ...
'FaceAlpha', error_alpha, 'EdgeColor', 'none');
end
end