定義函數(shù)
function h = errorshade(x,y,sigma,color,varargin)
%errorshade繪制陰影區(qū)域以指示高斯不確定性。
%通過生成指定顏色的RGB圖像并設(shè)置透明度來(lái)工作
%RGB圖像的百分比,對(duì)應(yīng)于不確定性值。
%
%% 語(yǔ)法規(guī)則
%
% errorshade(x,y,sigma,color)
% errorshade(...,'resolution',res)
% errorshade(...,'average',N)
% h = errorshade(...)
%
%% 簡(jiǎn)介
%
% %errorshade(x,y,sigma ,color)以高斯陰影區(qū)域?yàn)橹行睦L制
% 給定的行x,y。 輸入sigma表示陰影的一個(gè)標(biāo)準(zhǔn)偏差
% 重量百分比,顏色是包含陰影顏色的rgb值的三元素向量。
%
%errorshade(...,'resolution',res)指定基礎(chǔ)RGB圖像的分辨率。 資源
%可以是標(biāo)量值,也可以是[xres yres]形式的兩元素向量。 默認(rèn)分辨率
%是2500像素。 較大的值可能需要更長(zhǎng)的繪制時(shí)間,較小的值可能會(huì)出現(xiàn)抖動(dòng)。
%
%errorshade(...,'average',N)指定N點(diǎn)移動(dòng)平均值以平滑局部
%數(shù)據(jù)峰值百分比。 使用奇數(shù)整數(shù)值,因?yàn)榕紨?shù)會(huì)導(dǎo)致
%在水平方向上略有偏移。 平均選項(xiàng)需要圖像處理
% h = errorshade(...) returns a handle h of the plotted RGB image.
%
%% 舉例
%數(shù)據(jù)
% x = 0:10:500;
% y_true = 30*sind(x) + x/10;
% sigma = 3;
% y_measured = y_true + sigma*randn(size(x));
% plot(x,y_true,'k','linewidth',2)
% hold on
% plot(x,y_measured,'color',[0.0118 0.2078 0])
% ylabel 'some values in mV'
%
% % 繪制不確定性陰影區(qū)間
% errorshade(x,y_measured,sigma,[0.0824 0.6902 0.1020])
% legend('true value','measured value \pm\sigma = 3 mV uncertainty','location','northwest')
% legend boxoff
% axis tight
%% Error checks:
narginchk(4,inf)
assert(numel(color)==3,'輸入錯(cuò)誤: 顏色必須是三元素向量.')
assert(numel(x)==numel(y),'輸入錯(cuò)誤::x 和 y 維度必須相等')
assert(isscalar(sigma)==1,'輸入錯(cuò)誤: sigma必須是標(biāo)量.')
%% Input parsing
tmp = strncmpi(varargin,'resolution',3);
if any(tmp)
res = varargin{find(tmp)+1};
if isscalar(res)
res = [res res];
else
assert(numel(res)==2,'輸入錯(cuò)誤: 分辨率必須是標(biāo)量或二元素向量.')
end
else
res = 2500*[1 1];
end
tmp = strncmpi(varargin,'average',2);
if any(tmp)
avg = varargin{find(tmp)+1};
assert(isscalar(avg)==1,'輸入錯(cuò)誤: 移動(dòng)平均距離必須為標(biāo)量.')
y = imfilter(y(:),fspecial('average',[avg 1]),'replicate');
end
buffer = 3*sigma; % This is the padding to add around all measurements in the vertical dimension.
%% 限制范圍:
% 制作一個(gè)與數(shù)據(jù)緩沖區(qū)的尺寸相對(duì)應(yīng)的網(wǎng)格:
[X,Y] = meshgrid(linspace(min(x),max(x),res(1)),linspace(min(y)-buffer,max(y)+buffer,res(2)));
% Find y locations along all x points of the grid
yi = interp1(x,y,linspace(min(x),max(x),res(1)));
% 正態(tài)分布:
P = (1/sqrt(2*pi*sigma^2)) * exp(-(bsxfun(@minus,Y,yi)).^2/(2*sigma^2));
%每個(gè)點(diǎn)到y(tǒng)i的距離將用作透明度的量度:
Z = P-min(P(:));
Z = Z/max(Z(:));
%創(chuàng)建指定顏色的RGB圖像:
RGB = cat(3,color(1)*ones(size(Z)),color(2)*ones(size(Z)),color(3)*ones(size(Z)));
% 繪制顏色的RGB圖像:
h = image(RGB,'xdata',X(1,:),'ydata',Y(:,1));
axis xy
% 設(shè)置透明度:
set(h,'alphadata',Z)
% 將渲染器設(shè)置為OpenGL,因?yàn)橥该鞫葍H適用于OpenGL:
set(gcf,'renderer','OpenGL');
% 發(fā)送到底部:
uistack(h,'bottom')
%% 清除:
if nargout==0
clear h
end
end
測(cè)試代碼
clear,clc;
x = 0:10:500;
y_true = 30*sind(x) + x/10;
sigma = 3;
y_measured = y_true + sigma*randn(size(x)); plot(x,y_true,'k','linewidth',2)
hold on
plot(x,y_measured,'color',[0.0118 0.2078 0])
ylabel( 'Y軸')
% 繪制不確定性陰影區(qū)間
errorshade(x,y_measured,sigma,[1 0.4 0.7])
legend('真實(shí)值','測(cè)量值 \pm\sigma = 3 mV ','location','northwest')
legend box off
axis tight
繪制圖形