scatter_histhist.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. function [hm, hx, hy]=scatter_histhist(x, x_sig, y,y_sig, varargin)
  2. % [hm, hx, hy]=scatter_histhist(x, x_sig, y,y_sig, x_lim, y_lim)
  3. % Optional arguments:
  4. % width, hist_height, num_bins, x_label, y_label
  5. %
  6. % E.g
  7. % x = randn(150,1)*2;
  8. % y = randn(250,1)*4+3;
  9. % y = randn(150,1)*4+3;
  10. % xsig = abs(x)>2;
  11. % ysig = y<0;
  12. % draw.scatter_histhist(x,xsig,y,ysig,'x_label','X','y_label','Y')
  13. iod = @utils.inputordefault;
  14. org=iod('org',0.15, varargin);
  15. wdth=iod('width',0.5,varargin);
  16. hist_h=iod('hist_height',0.2,varargin);
  17. num_bns=iod('num_bins',17,varargin);
  18. x_label=iod('x_label','',varargin);
  19. y_label=iod('y_label','',varargin);
  20. x_lim_b=[min(x)-0.1 max(x)+0.1];
  21. y_lim_b=[min(y)-0.1 max(y)+0.1];
  22. x_lim = iod('x_lim',x_lim_b,varargin);
  23. y_lim = iod('y_lim',y_lim_b,varargin);
  24. figure
  25. hm=draw.jaxes;
  26. hm.Position = [org org wdth wdth];
  27. set(hm,'Xlim',[-1 1]);
  28. set(hm,'Ylim',[-1 1]);
  29. hx=axes('Position',[org org+wdth+0.01 wdth hist_h]);
  30. hy=axes('Position',[org+wdth+0.01 org hist_h wdth]);
  31. marker_size=zeros(size(x))+12;
  32. marker_size(x_sig==1)=24;
  33. marker_size(y_sig==1)=24;
  34. marker_size(x_sig+y_sig==2)=36;
  35. % make the scatter plot
  36. scatter(hm,x, y, 36,'k');
  37. xlabel(hm,x_label);
  38. ylabel(hm,y_label);
  39. xlim(hm,x_lim);
  40. ylim(hm,y_lim);
  41. draw.xhairs(hm,'k:',0,0);
  42. axes(hm);
  43. text(getx,gety,['n=' num2str(numel(x))])
  44. % make the y-axis histogram
  45. bns=linspace(y_lim_b(1),y_lim_b(2),num_bns);
  46. nsig=histc(y(y_sig==0), bns);
  47. nsig=nsig(1:end-1);
  48. sig=histc(y(y_sig==1), bns);
  49. sig=sig(1:end-1);
  50. cbns=edge2cen(bns);
  51. [hh]=barh(hy,cbns, [sig nsig],'stacked');
  52. set(hy, 'YTick',[]);
  53. ylim(hy,y_lim);
  54. set(hy, 'XAxisLocation','bottom');
  55. set(hh(1),'FaceColor','k')
  56. set(hh(2),'FaceColor',[1 1 1])
  57. set(hy,'box','off')
  58. set(hy,'Color','none')
  59. axes(hy);
  60. text(getx,gety,[num2str(round(100*mean(y_sig))) '% p<0.01'])
  61. y_mean=mean(y);
  62. [~,yt_sig]=ttest(y);
  63. set(hy,'NextPlot','add');
  64. xx=get(hy,'XLim');
  65. plot(hy,[0 xx(2)],[y_mean, y_mean],'-k');
  66. % make the x-axis histogram
  67. bns=linspace(x_lim_b(1),x_lim_b(2),num_bns);
  68. nsig=histc(x(x_sig==0), bns);
  69. nsig=nsig(1:end-1);
  70. sig=histc(x(x_sig==1), bns);
  71. sig=sig(1:end-1);
  72. cbns=edge2cen(bns);
  73. [hh]=bar(hx,cbns, [sig(:) nsig(:)],'stacked');
  74. set(hx, 'XTick',[]);
  75. xlim(hx,x_lim);
  76. set(gca, 'YAxisLocation','left');
  77. set(hh(1),'FaceColor','k')
  78. set(hh(2),'FaceColor',[1 1 1])
  79. set(hx,'box','off')
  80. set(hx,'Color','none')
  81. axes(hx);
  82. text(getx,gety,[num2str(round(100*mean(x_sig))) '% p<0.01'])
  83. x_mean=mean(x);
  84. [~,xt_sig]=ttest(x);
  85. set(hx,'NextPlot','add');
  86. yy=get(hx,'YLim');
  87. plot(hx, [x_mean, x_mean],[0 yy(2)],'-k');
  88. function y=edge2cen(x)
  89. b2b_dist=x(2)-x(1);
  90. y=x+0.5*b2b_dist;
  91. y=y(1:end-1);
  92. function y=getx
  93. x=xlim;
  94. y=0.1*(x(2)-x(1))+x(1);
  95. function y=gety
  96. x=ylim;
  97. y=0.9*(x(2)-x(1))+x(1);