Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 8674|回复: 0
打印 上一主题 下一主题

豆粑粑 matlab 画回归散点图 对应 hist图

[复制链接]

550

主题

629

帖子

3181

积分

论坛元老

Rank: 8Rank: 8

积分
3181
跳转到指定楼层
楼主
发表于 2017-1-12 21:40:44 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
一直想画这样的图。
似乎 matlab自己给出的scatterhist不容易设置想要的形式,特别是左下子图的大小,显示的方式 。
用ax来定义三个图的位置。
用dscatter函数(这是我下载的,调用别人的)
效果还不错。



主函数
  1. clear all
  2. clc
  3. clf

  4. h=fun_mm_reg_hist(reg_tr(:,1),reg_tr(:,2),lab_str);

  5. t = linspace(-1, 1.2, 2000);
  6. x = (t.^3)+(0.3.*randn(1, 2000));
  7. y = (t.^3)+(0.3.*randn(1, 2000))+(rand(1,2000)-0.5)*0.01;

  8. x=(x-min(x))/(max(x)-min(x));
  9. y=(y-min(y))/(max(y)-min(y));
  10. h=fun_mm_reg_hist(x',y',{'re','si'});
  11. fi_na=['../file_imgs/fig_demo'];
  12. fun_work_li_035_myfig_out(h,fi_na,3);
复制代码
我的函数
  1. function [h]=fun_mm_reg_hist(re,si,lab_str)

  2. n_grid=60;

  3. x_lin=linspace(0,1,n_grid);
  4. [x_grid,y_grid]=meshgrid(x_lin,x_lin);
  5. values = hist3([re si],{x_lin x_lin});
  6. values = medfilt2(values,[5,5]);
  7. values(values==0)=NaN;

  8. [hi_va_tr,hi_bi_tr]=hist(re,linspace(0,1,40));
  9. hi_va_tr=hi_va_tr./sum(hi_va_tr);
  10. [hi_va_si,hi_bi_si]=hist(si,linspace(0,1,40));
  11. hi_va_si=hi_va_si./sum(hi_va_si);

  12. h=figure(1);
  13. set(h, 'Position', [1000, 100, 800, 800]);
  14. ax=axes('position',[0.15 0.15 0.65 0.65]);
  15. % [val,h]=contourf(x_grid,y_grid,values','edgecolor','none');
  16. % shading interp
  17. % caxis([0,max(values(:))]);

  18. dscatter(re,si)
  19. % col_mm=flipud(hot);
  20. col_mm=jet;
  21. colormap(col_mm)
  22. % colorbar
  23. axis equal
  24. axis xy
  25. hold on

  26. % plot(re,si,'b.');
  27. plot([0 1],[0 1],'linewidth',2);
  28. view(0,90);
  29. set(gca,'fontsize',16,'xtick',[0:0.2:1],'ytick',[0:0.2:1])
  30. xlabel(lab_str{1});
  31. ylabel(lab_str{2});

  32. ax=axes('position',[0.15 0.8 0.65 0.15]);
  33. h_up=bar(hi_bi_tr,hi_va_tr,'b');
  34. set(gca,'xtick',[],'ytick',[],'xlim',[0 1]);

  35. ax=axes('position',[0.8 0.15 0.15 0.65]);
  36. h_right=barh(hi_bi_si,hi_va_si,'b');
  37. set(gca,'xtick',[],'ytick',[],'ylim',[0 1]);

  38. h=gcf;
复制代码
下载的函数 dscatter.m
  1. function hAxes = dscatter(X,Y, varargin)
  2. % DSCATTER creates a scatter plot coloured by density.
  3. %
  4. %   DSCATTER(X,Y) creates a scatterplot of X and Y at the locations
  5. %   specified by the vectors X and Y (which must be the same size), colored
  6. %   by the density of the points.
  7. %
  8. %   DSCATTER(...,'MARKER',M) allows you to set the marker for the
  9. %   scatter plot. Default is 's', square.
  10. %
  11. %   DSCATTER(...,'MSIZE',MS) allows you to set the marker size for the
  12. %   scatter plot. Default is 10.
  13. %
  14. %   DSCATTER(...,'FILLED',false) sets the markers in the scatter plot to be
  15. %   outline. The default is to use filled markers.
  16. %
  17. %   DSCATTER(...,'PLOTTYPE',TYPE) allows you to create other ways of
  18. %   plotting the scatter data. Options are "surf','mesh' and 'contour'.
  19. %   These create surf, mesh and contour plots colored by density of the
  20. %   scatter data.
  21. %
  22. %   DSCATTER(...,'BINS',[NX,NY]) allows you to set the number of bins used
  23. %   for the 2D histogram used to estimate the density. The default is to
  24. %   use the number of unique values in X and Y up to a maximum of 200.
  25. %
  26. %   DSCATTER(...,'SMOOTHING',LAMBDA) allows you to set the smoothing factor
  27. %   used by the density estimator. The default value is 20 which roughly
  28. %   means that the smoothing is over 20 bins around a given point.
  29. %
  30. %   DSCATTER(...,'LOGY',true) uses a log scale for the yaxis.
  31. %
  32. %   Examples:
  33. %
  34. %       [data, params] = fcsread('SampleFACS');
  35. %       dscatter(data(:,1),10.^(data(:,2)/256),'log',1)
  36. %       % Add contours
  37. %       hold on
  38. %       dscatter(data(:,1),10.^(data(:,2)/256),'log',1,'plottype','contour')
  39. %       hold off
  40. %       xlabel(params(1).LongName); ylabel(params(2).LongName);
  41. %      
  42. %   See also FCSREAD, SCATTER.

  43. % Copyright 2003-2004 The MathWorks, Inc.
  44. % $Revision:  [        DISCUZ_CODE_5        ]nbsp;  $Date:  $

  45. % Reference:
  46. % Paul H. C. Eilers and Jelle J. Goeman
  47. % Enhancing scatterplots with smoothed densities
  48. % Bioinformatics, Mar 2004; 20: 623 - 628.

  49. lambda = [];
  50. nbins = [];
  51. plottype = 'scatter';
  52. contourFlag = false;
  53. msize = 10;
  54. marker = 's';
  55. logy = false;
  56. filled = true;
  57. if nargin > 2
  58.     if rem(nargin,2) == 1
  59.         error('Bioinfo:IncorrectNumberOfArguments',...
  60.             'Incorrect number of arguments to %s.',mfilename);
  61.     end
  62.     okargs = {'smoothing','bins','plottype','logy','marker','msize','filled'};
  63.     for j=1:2:nargin-2
  64.         pname = varargin{j};
  65.         pval = varargin{j+1};
  66.         k = strmatch(lower(pname), okargs); %#ok
  67.         if isempty(k)
  68.             error('Bioinfo:UnknownParameterName',...
  69.                 'Unknown parameter name: %s.',pname);
  70.         elseif length(k)>1
  71.             error('Bioinfo:AmbiguousParameterName',...
  72.                 'Ambiguous parameter name: %s.',pname);
  73.         else
  74.             switch(k)
  75.                 case 1  % smoothing factor
  76.                     if isnumeric(pval)
  77.                         lambda = pval;
  78.                     else
  79.                         error('Bioinfo:InvalidScoringMatrix','Invalid smoothing parameter.');
  80.                     end
  81.                 case 2
  82.                     if isscalar(pval)
  83.                         nbins = [ pval pval];
  84.                     else
  85.                         nbins = pval;
  86.                     end
  87.                 case 3
  88.                     plottype = pval;
  89.                 case 4
  90.                     logy = pval;
  91.                     Y = log10(Y);
  92.                 case 5
  93.                     contourFlag = pval;
  94.                 case 6
  95.                     marker = pval;
  96.                 case 7
  97.                     msize = pval;
  98.                 case 8
  99.                     filled = pval;
  100.             end
  101.         end
  102.     end
  103. end

  104. minx = min(X,[],1);
  105. maxx = max(X,[],1);
  106. miny = min(Y,[],1);
  107. maxy = max(Y,[],1);

  108. if isempty(nbins)
  109.     nbins = [min(numel(unique(X)),200) ,min(numel(unique(Y)),200) ];
  110. end

  111. if isempty(lambda)
  112.     lambda = 20;
  113. end

  114. edges1 = linspace(minx, maxx, nbins(1)+1);
  115. ctrs1 = edges1(1:end-1) + .5*diff(edges1);
  116. edges1 = [-Inf edges1(2:end-1) Inf];
  117. edges2 = linspace(miny, maxy, nbins(2)+1);
  118. ctrs2 = edges2(1:end-1) + .5*diff(edges2);
  119. edges2 = [-Inf edges2(2:end-1) Inf];

  120. [n,p] = size(X);
  121. bin = zeros(n,2);
  122. % Reverse the columns to put the first column of X along the horizontal
  123. % axis, the second along the vertical.
  124. [dum,bin(:,2)] = histc(X,edges1);
  125. [dum,bin(:,1)] = histc(Y,edges2);
  126. H = accumarray(bin,1,nbins([2 1])) ./ n;
  127. G = smooth1D(H,nbins(2)/lambda);
  128. F = smooth1D(G',nbins(1)/lambda)';
  129. % = filter2D(H,lambda);

  130. if logy
  131.     ctrs2 = 10.^ctrs2;
  132.     Y = 10.^Y;
  133. end
  134. okTypes = {'surf','mesh','contour','image','scatter'};
  135. k = strmatch(lower(plottype), okTypes); %#ok
  136. if isempty(k)
  137.     error('dscatter:UnknownPlotType',...
  138.         'Unknown plot type: %s.',plottype);
  139. elseif length(k)>1
  140.     error('dscatter:AmbiguousPlotType',...
  141.         'Ambiguous plot type: %s.',plottype);
  142. else
  143.     switch(k)

  144.         case 1 %'surf'
  145.             fmax=max(F(:));
  146.              F(F<fmax/100)=NaN;
  147.             h = surf(ctrs1,ctrs2,F,'edgealpha',0);
  148.         case 2 % 'mesh'
  149.             h = mesh(ctrs1,ctrs2,F);
  150.         case 3 %'contour'
  151.             [dummy, h] =contour(ctrs1,ctrs2,F);
  152.         case 4 %'image'
  153.             nc = 256;
  154.             F = F./max(F(:));
  155.             colormap(repmat(linspace(1,0,nc)',1,3));
  156.             h =image(ctrs1,ctrs2,floor(nc.*F) + 1);
  157.         case 5 %'scatter'
  158.             F = F./max(F(:));
  159.             ind = sub2ind(size(F),bin(:,1),bin(:,2));
  160.             col = F(ind);
  161.             if filled
  162.                 h = scatter(X,Y,msize,col,marker,'filled');
  163.             else
  164.                 h = scatter(X,Y,msize,col,marker);
  165.             end
  166.     end

  167. end

  168. if logy
  169.     set(gca,'yscale','log');
  170. end
  171. if nargout > 0
  172.     hAxes = get(h,'parent');
  173. end
  174. %%%% This method is quicker for symmetric data.
  175. % function Z = filter2D(Y,bw)
  176. % z = -1:(1/bw):1;
  177. % k = .75 * (1 - z.^2);
  178. % k = k ./ sum(k);
  179. % Z = filter2(k'*k,Y);

  180. function Z = smooth1D(Y,lambda)
  181. [m,n] = size(Y);
  182. E = eye(m);
  183. D1 = diff(E,1);
  184. D2 = diff(D1,1);
  185. P = lambda.^2 .* D2'*D2 + 2.*lambda .* D1'*D1;
  186. Z = (E + P) \ Y;
复制代码





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Mathematica中文论坛-非官方 ( 辽ICP备16001491号-1

GMT+8, 2024-12-29 21:55 , Processed in 0.110291 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表