Matlab畫帶有陰影的誤差圖

定義函數(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 

繪制圖形

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天的溫度很低,兩個(gè)孩子都跟我們一起去老家,老家的雪還有很多,她們一起玩打雪仗! 今天下午,雪花又開始飄落下來(lái)!雖...
    沐浴陽(yáng)光_ee71閱讀 195評(píng)論 0 2
  • “今天是你的生日,放學(xué)后再點(diǎn)回家,媽媽給你做了好多你愛吃的?!?陸敏一邊在和女兒蒂蒂談電話,一邊回憶起前不久患了帕...
    荒誕行軍閱讀 414評(píng)論 0 0

友情鏈接更多精彩內(nèi)容