shadedErrorBar.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. function varargout=shadedErrorBar(x,y,errBar,lineProps,transparent)
  2. % function H=shadedErrorBar(x,y,errBar,lineProps,transparent)
  3. %
  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. %
  8. % Inputs
  9. % x - vector of x values [optional, can be left empty]
  10. % y - vector of y values or a matrix of n observations by m cases
  11. % where m has length(x);
  12. % errBar - if a vector we draw symmetric errorbars. If it has a size
  13. % of [2,length(x)] then we draw asymmetric error bars with
  14. % row 1 being the upper bar and row 2 being the lower bar
  15. % (with respect to y). ** alternatively ** errBar can be a
  16. % cellArray of two function handles. The first defines which
  17. % statistic the line should be and the second defines the
  18. % error bar.
  19. % lineProps - [optional,'-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 - [optional, 0 by default] if ==1 the shaded error
  23. % bar is made transparent, which forces the renderer
  24. % to be openGl. However, if this is saved as .eps the
  25. % resulting file will contain a raster not a vector
  26. % image.
  27. %
  28. % Outputs
  29. % H - a structure of handles to the generated plot objects.
  30. %
  31. %
  32. % Examples
  33. % y=randn(30,80); x=1:size(y,2);
  34. % shadedErrorBar(x,mean(y,1),std(y),'g');
  35. % shadedErrorBar(x,y,{@median,@std},{'r-o','markerfacecolor','r'});
  36. % shadedErrorBar([],y,{@median,@std},{'r-o','markerfacecolor','r'});
  37. %
  38. % Overlay two transparent lines
  39. % y=randn(30,80)*10; x=(1:size(y,2))-40;
  40. % shadedErrorBar(x,y,{@mean,@std},'-r',1);
  41. % hold on
  42. % y=ones(30,1)*x; y=y+0.06*y.^2+randn(size(y))*10;
  43. % shadedErrorBar(x,y,{@mean,@std},'-b',1);
  44. % hold off
  45. %
  46. %
  47. % Rob Campbell - November 2009
  48. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  49. % Error checking
  50. error(nargchk(3,5,nargin))
  51. %Process y using function handles if needed to make the error bar
  52. %dynamically
  53. if iscell(errBar)
  54. fun1=errBar{1};
  55. fun2=errBar{2};
  56. errBar=fun2(y);
  57. y=fun1(y);
  58. else
  59. y=y(:)';
  60. end
  61. if isempty(x)
  62. x=1:length(y);
  63. else
  64. x=x(:)';
  65. end
  66. %Make upper and lower error bars if only one was specified
  67. if length(errBar)==length(errBar(:))
  68. errBar=repmat(errBar(:)',2,1);
  69. else
  70. s=size(errBar);
  71. f=find(s==2);
  72. if isempty(f), error('errBar has the wrong size'), end
  73. if f==2, errBar=errBar'; end
  74. end
  75. if length(x) ~= length(errBar)
  76. error('length(x) must equal length(errBar)')
  77. end
  78. %Set default options
  79. defaultProps={'-k'};
  80. if nargin<4, lineProps=defaultProps; end
  81. if isempty(lineProps), lineProps=defaultProps; end
  82. if ~iscell(lineProps), lineProps={lineProps}; end
  83. if nargin<5, transparent=0; end
  84. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  85. % Plot to get the parameters of the line
  86. H.mainLine=plot(x,y,lineProps{:});
  87. % Work out the color of the shaded region and associated lines
  88. % Using alpha requires the render to be openGL and so you can't
  89. % save a vector image. On the other hand, you need alpha if you're
  90. % overlaying lines. There we have the option of choosing alpha or a
  91. % de-saturated solid colour for the patch surface .
  92. col=get(H.mainLine,'color');
  93. edgeColor=col+(1-col)*0.55;
  94. patchSaturation=0.15; %How de-saturated or transparent to make patch
  95. if transparent
  96. faceAlpha=patchSaturation;
  97. patchColor=col;
  98. set(gcf,'renderer','openGL')
  99. else
  100. faceAlpha=1;
  101. patchColor=col+(1-col)*(1-patchSaturation);
  102. set(gcf,'renderer','painters')
  103. end
  104. %Calculate the error bars
  105. uE=y+errBar(1,:);
  106. lE=y-errBar(2,:);
  107. %Add the patch error bar
  108. holdStatus=ishold;
  109. if ~holdStatus, hold on, end
  110. %Make the patch
  111. yP=[lE,fliplr(uE)];
  112. xP=[x,fliplr(x)];
  113. %remove nans otherwise patch won't work
  114. xP(isnan(yP))=[];
  115. yP(isnan(yP))=[];
  116. H.patch=patch(xP,yP,1,'facecolor',patchColor,...
  117. 'edgecolor','none',...
  118. 'facealpha',faceAlpha);
  119. %Make pretty edges around the patch.
  120. H.edge(1)=plot(x,lE,'-','color',edgeColor);
  121. H.edge(2)=plot(x,uE,'-','color',edgeColor);
  122. %Now replace the line (this avoids having to bugger about with z coordinates)
  123. delete(H.mainLine)
  124. H.mainLine=plot(x,y,lineProps{:});
  125. if ~holdStatus, hold off, end
  126. if nargout==1
  127. varargout{1}=H;
  128. end