误差表示errorbar and  shadederrorbar

误差表示errorbar and  shadederrorbar

1. errorbar

ErrorBar(误差棒图),是统计学中常用的图形。ErrorBar图涉及到数据的“平均值”和“标准差”。

         下面举例子理解误差棒图中涉及到的“平均值”和“标准差”。

         某地降雨量的误差棒图[1]如图1所示,从横纵1月份的刻度值往正上查找时,可发现1月份的降雨量对应于一个“工”字型的图案。该“工”字型的图案的中间的点对应的纵轴的刻度值(大约12),表示该地1月份的降雨量的“平均值”,大约12cm。而“工”字型图案的上横线或下横线所对应的纵轴的刻度值到中间点(即均值)的差值大约为0.5,表示该地1月份的降雨量的“标准差”大约为0.5cm。

         

图 1 某地降雨量的误差棒图[1]

图1的例子来自参考文献[1],matlab代码如下

 

 %各月的平均值
 Average=[12,11,7,7,6,5];  
 %各月的标准值
 Variance=[0.5,0.4,0.3,1,0.3,0.5];   
 Time=1:1:6;
 %函数调用格式 errorbar(A,B,X)
 errorbar(Time,Average,Variance)    
 xlabel('月份');
 ylabel('降雨量/cm');

 

图1显示的是纵向“工”字型图案的errorbar,并且“工”字型图案的上下横线到中点的距离相等。参考文献[1]中还有其他形式的纵向的errbar的画法,比如“工”字型图案的上下横线到中点的距离

不想等的情况。参考文献[2]中还有横向的“工”字型图案的errorbar的说明。可点击参考文献[1][2]了解更多关于errorbar的内容。

参考文献:

1.《Matlab绘制误差棒图(errorbar函数的使用)》

2.matlab官方关于函数errorbar说明

 

2.shadederrorbar

 

 

shadedErrorBar是一个MATLAB函数,它在一条线而不是离散条上​​创建一个连续的阴影误差区域。可以显式指定错误区域,也可以根据函数句柄动态计算错误区域。可以通过输入参数和/或返回的绘图对象的句柄来修改绘图的外观。

shadedErrorBar,并不是matlab官方提供的api函数,而是一位大佬提供的,其开源代码和英文介绍可查阅参考文献[3]。从某种意义上来说,与ErrorBar不同的是,ErrorBar图中“标准差”和“均值”离散分布的,而在shadeErrorBar中则是连续分布的,

1、首先准备下载shadedErrorBar.m函数代码,github下载

在此我给出已经下载的代码

 
  1. function varargout=shadedErrorBar(x,y,errBar,varargin)

  2. % generate continuous error bar area around a line plot

  3. % function H=shadedErrorBar(x,y,errBar, ...)

  4. % Purpose

  5. % Makes a 2-d line plot with a pretty shaded error bar made

  6. % using patch. Error bar color is chosen automatically.

  7. % Inputs (required)

  8. % x - vector of x values [optional, can be left empty]

  9. % y - vector of y values or a matrix of n observations by m cases

  10. % where m has length(x);

  11. % errBar - if a vector we draw symmetric errorbars. If it has a size

  12. % of [2,length(x)] then we draw asymmetric error bars with

  13. % row 1 being the upper bar and row 2 being the lower bar

  14. % (with respect to y -- see demo). ** alternatively **

  15. % errBar can be a cellArray of two function handles. The

  16. % first defines statistic the line should be and the second

  17. % defines the error bar.

  18. % Inputs (optional, param/value pairs)

  19. % 'lineProps' - ['-k' by default] defines the properties of

  20. % the data line. e.g.:

  21. % 'or-', or {'-or','markerfacecolor',[1,0.2,0.2]}

  22. % 'transparent' - [true by default] if true, the shaded error

  23. % bar is made transparent. However, for a transparent

  24. % vector image you will need to save as PDF, not EPS,

  25. % and set the figure renderer to "painters". An EPS

  26. % will only be transparent if you set the renderer

  27. % to OpenGL, however this makes a raster image.

  28. % 'patchSaturation'- [0.2 by default] The saturation of the patch color.

  29. % Outputs

  30. % H - a structure of handles to the generated plot objects.

  31. % Examples:

  32. % y=randn(30,80);

  33. % x=1:size(y,2);

  34. % 1)

  35. % shadedErrorBar(x,mean(y,1),std(y),'lineprops','g');

  36. % 2)

  37. % shadedErrorBar(x,y,{@median,@std},'lineprops',{'r-o','markerfacecolor','r'});

  38. %

  39. % 3)

  40. % shadedErrorBar([],y,{@median,@(x) std(x)*1.96},'lineprops',{'r-o','markerfacecolor','k'});

  41. % 4)

  42. % Overlay two transparent lines:

  43. % clf

  44. % y=randn(30,80)*10;

  45. % x=(1:size(y,2))-40;

  46. % shadedErrorBar(x,y,{@mean,@std},'lineprops','-r','transparent',1);

  47. % hold on

  48. % y=ones(30,1)*x; y=y+0.06*y.^2+randn(size(y))*10;

  49. % shadedErrorBar(x,y,{@mean,@std},'lineprops','-b','transparent',1);

  50. % hold off

  51. %

  52. %

  53. % Rob Campbell - November 2009

  54. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  55. % Parse input arguments

  56. narginchk(3,inf)

  57. params = inputParser;

  58. params.CaseSensitive = false;

  59. params.addParameter('lineProps', '-k', @(x) ischar(x) | iscell(x));

  60. params.addParameter('transparent', true, @(x) islogical(x) || x==0 || x==1);

  61. params.addParameter('patchSaturation', 0.2, @(x) isnumeric(x) && x>=0 && x<=1);

  62. params.parse(varargin{:});

  63. %Extract values from the inputParser

  64. lineProps = params.Results.lineProps;

  65. transparent = params.Results.transparent;

  66. patchSaturation = params.Results.patchSaturation;

  67. if ~iscell(lineProps), lineProps={lineProps}; end

  68. %Process y using function handles if needed to make the error bar dynamically

  69. if iscell(errBar)

  70. fun1=errBar{1};

  71. fun2=errBar{2};

  72. errBar=fun2(y);

  73. y=fun1(y);

  74. else

  75. y=y(:).';

  76. end

  77.  
  78. if isempty(x)

  79. x=1:length(y);

  80. else

  81. x=x(:).';

  82. end

  83. %Make upper and lower error bars if only one was specified

  84. if length(errBar)==length(errBar(:))

  85. errBar=repmat(errBar(:)',2,1);

  86. else

  87. s=size(errBar);

  88. f=find(s==2);

  89. if isempty(f), error('errBar has the wrong size'), end

  90. if f==2, errBar=errBar'; end

  91. end

  92. if length(x) ~= length(errBar)

  93. error('length(x) must equal length(errBar)')

  94. end

  95. %Log the hold status so we don't change

  96. initialHoldStatus=ishold;

  97. if ~initialHoldStatus, hold on, end

  98. H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation);

  99. if ~initialHoldStatus, hold off, end

  100. if nargout==1

  101. varargout{1}=H;

  102. end

  103.  
  104. function H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation)

  105.  
  106. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  107. % Plot to get the parameters of the line

  108. H.mainLine=plot(x,y,lineProps{:});

  109. % Work out the color of the shaded region and associated lines.

  110. % Here we have the option of choosing alpha or a de-saturated

  111. % solid colour for the patch surface.

  112. mainLineColor=get(H.mainLine,'color');

  113. edgeColor=mainLineColor+(1-mainLineColor)*0.55;

  114. if transparent

  115. faceAlpha=patchSaturation;

  116. patchColor=mainLineColor;

  117. else

  118. faceAlpha=1;

  119. patchColor=mainLineColor+(1-mainLineColor)*(1-patchSaturation);

  120. end

  121. %Calculate the error bars

  122. uE=y+errBar(1,:);

  123. lE=y-errBar(2,:);

  124. %Add the patch error bar

  125. %Make the patch

  126. yP=[lE,fliplr(uE)];

  127. xP=[x,fliplr(x)];

  128. %remove nans otherwise patch won't work

  129. xP(isnan(yP))=[];

  130. yP(isnan(yP))=[];

  131. if(isdatetime(x))

  132. H.patch=patch(datenum(xP),yP,1);

  133. else

  134. H.patch=patch(xP,yP,1);

  135. end

  136. set(H.patch,'facecolor',patchColor, ...

  137. 'edgecolor','none', ...

  138. 'facealpha',faceAlpha)

  139. %Make pretty edges around the patch.

  140. H.edge(1)=plot(x,lE,'-','color',edgeColor);

  141. H.edge(2)=plot(x,uE,'-','color',edgeColor);

  142. uistack(H.mainLine,'top') % Bring the main line to the top

 

2、选择shadedErrorBar.m文件黏贴到matalb的toolbox文件下面,我设置的路径是D:\Program Files\MATLAB\R2016b\toolbox\shadedErrorBaryy。然后选择matlab主页的设置路径

3、将预先设置的文件添加进去

 

4、检查是否设置成功

     在matlab运行窗口输入help shadedErrorBar,如果出现以下情况说明 shadedErrorBar函数设置成功

 

5、最后运行 

 
  1. % Prepare data

  2. y=randn(30,80)*5;

  3. x=(1:size(y,2))-40;

  4. yP = sin( linspace(-2*pi,2*pi,length(x)) )*20;

  5. y = bsxfun(@plus,y,yP)+60;

  6. % Make the plot

  7. clf

  8. shadedErrorBar(x,y,{@mean,@std});

  9. % Overlay the raw data

  10. hold on

  11. plot(x,y,'.','color',[0.5,0.5,0.95])

  12. grid on

 

 
  1. % Prepare data for first line

  2. y=ones(30,1)*x;

  3. y=y+0.06*y.^2+randn(size(y))*10;

  4. clf

  5. shadedErrorBar(x,y,{@mean,@std},'lineprops','-b','patchSaturation',0.33)

  6. % Overlay second line

  7. hold on

  8. shadedErrorBar(x,2*y+20,{@mean,@std},'lineprops',{'-go','MarkerFaceColor','g'});

  9. %Overlay third line

  10. y=randn(30,80)*5;

  11. x=(1:size(y,2))-40;

  12. yP = sin( linspace(-2*pi,2*pi,length(x)) )*20;

  13. y = bsxfun(@plus,y,yP)+60;

  14. % Make this line non-transparent

  15. shadedErrorBar(x, y, {@mean,@std}, 'lineprops', '-r','transparent',false,'patchSaturation',0.075)

  16. grid on

 

 

 
  1. % Prepare data

  2. y=randn(30,80)*5;

  3. x=(1:size(y,2));

  4. yP = sin( linspace(-2*pi,2*pi,length(x)) )*20;

  5. y = bsxfun(@plus,y,yP);

  6. % Plot

  7. clf

  8. s = shadedErrorBar(x, y, {@mean,@std}, 'lineprops', '-r');

  9. % Set face and edge properties

  10. set(s.edge,'LineWidth',2,'LineStyle',':')

  11. s.mainLine.LineWidth = 5;

  12. s.patch.FaceColor = [0.5,0.25,0.25];

  13. % Overlay data points post-hoc

  14. hold on

  15. plot(s.mainLine.XData, s.mainLine.YData,'or','MarkerFaceColor','w')

  16. grid on

  17. set(gca,'XTickLabel',[],'YTickLabel',[])

 

 
  1. % Post-hoc modifications of line properties

  2. y=randn(256,80)*5;

  3. x=(1:size(y,2));

  4. yP = cos( linspace(-2*pi,2*pi,length(x)) )*10;

  5. y = bsxfun(@plus,y,yP);

  6. shadedErrorBar(x, y, {@mean,@std}, 'lineprops', '-r')

  7. hold on

  8. y=mean(y)+16;

  9. errbar = [2*ones(1,length(x)) ; 4*ones(1,length(x))];

  10. shadedErrorBar(x, y, errbar, 'lineprops', '-g')

 

3.shadedErrorBar在github上的说明

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值