Matlab画带有阴影的误差图

定义函数

function h = errorshade(x,y,sigma,color,varargin)
%errorshade绘制阴影区域以指示高斯不确定性。 
%通过生成指定颜色的RGB图像并设置透明度来工作
%RGB图像的百分比,对应于不确定性值。
% 
%% 语法规则
% 
%  errorshade(x,y,sigma,color) 
%  errorshade(...,'resolution',res) 
%  errorshade(...,'average',N)
%  h = errorshade(...)
% 
%% 简介
%
% %errorshade(x,y,sigma ,color)以高斯阴影区域为中心绘制
% 给定的行x,y。 输入sigma表示阴影的一个标准偏差
% 重量百分比,颜色是包含阴影颜色的rgb值的三元素向量。
%
%errorshade(...,'resolution',res)指定基础RGB图像的分辨率。 资源
%可以是标量值,也可以是[xres yres]形式的两元素向量。 默认分辨率
%是2500像素。 较大的值可能需要更长的绘制时间,较小的值可能会出现抖动。
%
%errorshade(...,'average',N)指定N点移动平均值以平滑局部
%数据峰值百分比。 使用奇数整数值,因为偶数会导致
%在水平方向上略有偏移。 平均选项需要图像处理
% h = errorshade(...) returns a handle h of the plotted RGB image. 
%
%% 举例
%数据
% 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'
% 
% % 绘制不确定性阴影区间
% 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,'输入错误: 颜色必须是三元素向量.')
assert(numel(x)==numel(y),'输入错误::x 和 y 维度必须相等') 
assert(isscalar(sigma)==1,'输入错误: sigma必须是标量.') 

%% 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,'输入错误: 分辨率必须是标量或二元素向量.') 
   end
else
   res = 2500*[1 1]; 
end

tmp = strncmpi(varargin,'average',2); 
if any(tmp)
   avg = varargin{find(tmp)+1}; 
   assert(isscalar(avg)==1,'输入错误: 移动平均距离必须为标量.')
   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. 

%% 限制范围: 

% 制作一个与数据缓冲区的尺寸相对应的网格: 
[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)));

% 正态分布: 
P = (1/sqrt(2*pi*sigma^2)) * exp(-(bsxfun(@minus,Y,yi)).^2/(2*sigma^2));

%每个点到yi的距离将用作透明度的量度:
Z = P-min(P(:));
Z = Z/max(Z(:)); 

%创建指定颜色的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 

% 设置透明度: 
set(h,'alphadata',Z)

% 将渲染器设置为OpenGL,因为透明度仅适用于OpenGL: 
set(gcf,'renderer','OpenGL'); 

% 发送到底部: 
uistack(h,'bottom')

%% 清除: 

if nargout==0 
   clear h
end

end

测试代码

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轴')
% 绘制不确定性阴影区间
errorshade(x,y_measured,sigma,[1 0.4 0.7]) 
legend('真实值','测量值 \pm\sigma = 3 mV ','location','northwest')
legend box off 
axis tight 

绘制图形

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天的温度很低,两个孩子都跟我们一起去老家,老家的雪还有很多,她们一起玩打雪仗! 今天下午,雪花又开始飘落下来!虽...
    沐浴阳光_ee71阅读 181评论 0 2
  • “今天是你的生日,放学后再点回家,妈妈给你做了好多你爱吃的。” 陆敏一边在和女儿蒂蒂谈电话,一边回忆起前不久患了帕...
    荒诞行军阅读 375评论 0 0