Mathematica中文论坛-非官方
标题:
豆粑粑 matlab 画回归散点图 对应 hist图
[打印本页]
作者:
meatball1982
时间:
2017-1-12 21:40
标题:
豆粑粑 matlab 画回归散点图 对应 hist图
一直想画这样的图。
似乎 matlab自己给出的
scatterhist不容易设置想要的形式,特别是左下子图的大小,显示的方式 。
用ax来定义三个图的位置。
用dscatter函数(这是我下载的,调用别人的)
效果还不错。
fig_demo.png
(160.79 KB, 下载次数: 1288)
下载附件
2017-1-12 21:38 上传
主函数
clear all
clc
clf
h=fun_mm_reg_hist(reg_tr(:,1),reg_tr(:,2),lab_str);
t = linspace(-1, 1.2, 2000);
x = (t.^3)+(0.3.*randn(1, 2000));
y = (t.^3)+(0.3.*randn(1, 2000))+(rand(1,2000)-0.5)*0.01;
x=(x-min(x))/(max(x)-min(x));
y=(y-min(y))/(max(y)-min(y));
h=fun_mm_reg_hist(x',y',{'re','si'});
fi_na=['../file_imgs/fig_demo'];
fun_work_li_035_myfig_out(h,fi_na,3);
复制代码
我的函数
function [h]=fun_mm_reg_hist(re,si,lab_str)
n_grid=60;
x_lin=linspace(0,1,n_grid);
[x_grid,y_grid]=meshgrid(x_lin,x_lin);
values = hist3([re si],{x_lin x_lin});
values = medfilt2(values,[5,5]);
values(values==0)=NaN;
[hi_va_tr,hi_bi_tr]=hist(re,linspace(0,1,40));
hi_va_tr=hi_va_tr./sum(hi_va_tr);
[hi_va_si,hi_bi_si]=hist(si,linspace(0,1,40));
hi_va_si=hi_va_si./sum(hi_va_si);
h=figure(1);
set(h, 'Position', [1000, 100, 800, 800]);
ax=axes('position',[0.15 0.15 0.65 0.65]);
% [val,h]=contourf(x_grid,y_grid,values','edgecolor','none');
% shading interp
% caxis([0,max(values(:))]);
dscatter(re,si)
% col_mm=flipud(hot);
col_mm=jet;
colormap(col_mm)
% colorbar
axis equal
axis xy
hold on
% plot(re,si,'b.');
plot([0 1],[0 1],'linewidth',2);
view(0,90);
set(gca,'fontsize',16,'xtick',[0:0.2:1],'ytick',[0:0.2:1])
xlabel(lab_str{1});
ylabel(lab_str{2});
ax=axes('position',[0.15 0.8 0.65 0.15]);
h_up=bar(hi_bi_tr,hi_va_tr,'b');
set(gca,'xtick',[],'ytick',[],'xlim',[0 1]);
ax=axes('position',[0.8 0.15 0.15 0.65]);
h_right=barh(hi_bi_si,hi_va_si,'b');
set(gca,'xtick',[],'ytick',[],'ylim',[0 1]);
h=gcf;
复制代码
下载的函数 dscatter.m
function hAxes = dscatter(X,Y, varargin)
% DSCATTER creates a scatter plot coloured by density.
%
% DSCATTER(X,Y) creates a scatterplot of X and Y at the locations
% specified by the vectors X and Y (which must be the same size), colored
% by the density of the points.
%
% DSCATTER(...,'MARKER',M) allows you to set the marker for the
% scatter plot. Default is 's', square.
%
% DSCATTER(...,'MSIZE',MS) allows you to set the marker size for the
% scatter plot. Default is 10.
%
% DSCATTER(...,'FILLED',false) sets the markers in the scatter plot to be
% outline. The default is to use filled markers.
%
% DSCATTER(...,'PLOTTYPE',TYPE) allows you to create other ways of
% plotting the scatter data. Options are "surf','mesh' and 'contour'.
% These create surf, mesh and contour plots colored by density of the
% scatter data.
%
% DSCATTER(...,'BINS',[NX,NY]) allows you to set the number of bins used
% for the 2D histogram used to estimate the density. The default is to
% use the number of unique values in X and Y up to a maximum of 200.
%
% DSCATTER(...,'SMOOTHING',LAMBDA) allows you to set the smoothing factor
% used by the density estimator. The default value is 20 which roughly
% means that the smoothing is over 20 bins around a given point.
%
% DSCATTER(...,'LOGY',true) uses a log scale for the yaxis.
%
% Examples:
%
% [data, params] = fcsread('SampleFACS');
% dscatter(data(:,1),10.^(data(:,2)/256),'log',1)
% % Add contours
% hold on
% dscatter(data(:,1),10.^(data(:,2)/256),'log',1,'plottype','contour')
% hold off
% xlabel(params(1).LongName); ylabel(params(2).LongName);
%
% See also FCSREAD, SCATTER.
% Copyright 2003-2004 The MathWorks, Inc.
% $Revision: [ DISCUZ_CODE_5 ]nbsp; $Date: $
% Reference:
% Paul H. C. Eilers and Jelle J. Goeman
% Enhancing scatterplots with smoothed densities
% Bioinformatics, Mar 2004; 20: 623 - 628.
lambda = [];
nbins = [];
plottype = 'scatter';
contourFlag = false;
msize = 10;
marker = 's';
logy = false;
filled = true;
if nargin > 2
if rem(nargin,2) == 1
error('Bioinfo:IncorrectNumberOfArguments',...
'Incorrect number of arguments to %s.',mfilename);
end
okargs = {'smoothing','bins','plottype','logy','marker','msize','filled'};
for j=1:2:nargin-2
pname = varargin{j};
pval = varargin{j+1};
k = strmatch(lower(pname), okargs); %#ok
if isempty(k)
error('Bioinfo:UnknownParameterName',...
'Unknown parameter name: %s.',pname);
elseif length(k)>1
error('Bioinfo:AmbiguousParameterName',...
'Ambiguous parameter name: %s.',pname);
else
switch(k)
case 1 % smoothing factor
if isnumeric(pval)
lambda = pval;
else
error('Bioinfo:InvalidScoringMatrix','Invalid smoothing parameter.');
end
case 2
if isscalar(pval)
nbins = [ pval pval];
else
nbins = pval;
end
case 3
plottype = pval;
case 4
logy = pval;
Y = log10(Y);
case 5
contourFlag = pval;
case 6
marker = pval;
case 7
msize = pval;
case 8
filled = pval;
end
end
end
end
minx = min(X,[],1);
maxx = max(X,[],1);
miny = min(Y,[],1);
maxy = max(Y,[],1);
if isempty(nbins)
nbins = [min(numel(unique(X)),200) ,min(numel(unique(Y)),200) ];
end
if isempty(lambda)
lambda = 20;
end
edges1 = linspace(minx, maxx, nbins(1)+1);
ctrs1 = edges1(1:end-1) + .5*diff(edges1);
edges1 = [-Inf edges1(2:end-1) Inf];
edges2 = linspace(miny, maxy, nbins(2)+1);
ctrs2 = edges2(1:end-1) + .5*diff(edges2);
edges2 = [-Inf edges2(2:end-1) Inf];
[n,p] = size(X);
bin = zeros(n,2);
% Reverse the columns to put the first column of X along the horizontal
% axis, the second along the vertical.
[dum,bin(:,2)] = histc(X,edges1);
[dum,bin(:,1)] = histc(Y,edges2);
H = accumarray(bin,1,nbins([2 1])) ./ n;
G = smooth1D(H,nbins(2)/lambda);
F = smooth1D(G',nbins(1)/lambda)';
% = filter2D(H,lambda);
if logy
ctrs2 = 10.^ctrs2;
Y = 10.^Y;
end
okTypes = {'surf','mesh','contour','image','scatter'};
k = strmatch(lower(plottype), okTypes); %#ok
if isempty(k)
error('dscatter:UnknownPlotType',...
'Unknown plot type: %s.',plottype);
elseif length(k)>1
error('dscatter:AmbiguousPlotType',...
'Ambiguous plot type: %s.',plottype);
else
switch(k)
case 1 %'surf'
fmax=max(F(:));
F(F<fmax/100)=NaN;
h = surf(ctrs1,ctrs2,F,'edgealpha',0);
case 2 % 'mesh'
h = mesh(ctrs1,ctrs2,F);
case 3 %'contour'
[dummy, h] =contour(ctrs1,ctrs2,F);
case 4 %'image'
nc = 256;
F = F./max(F(:));
colormap(repmat(linspace(1,0,nc)',1,3));
h =image(ctrs1,ctrs2,floor(nc.*F) + 1);
case 5 %'scatter'
F = F./max(F(:));
ind = sub2ind(size(F),bin(:,1),bin(:,2));
col = F(ind);
if filled
h = scatter(X,Y,msize,col,marker,'filled');
else
h = scatter(X,Y,msize,col,marker);
end
end
end
if logy
set(gca,'yscale','log');
end
if nargout > 0
hAxes = get(h,'parent');
end
%%%% This method is quicker for symmetric data.
% function Z = filter2D(Y,bw)
% z = -1:(1/bw):1;
% k = .75 * (1 - z.^2);
% k = k ./ sum(k);
% Z = filter2(k'*k,Y);
function Z = smooth1D(Y,lambda)
[m,n] = size(Y);
E = eye(m);
D1 = diff(E,1);
D2 = diff(D1,1);
P = lambda.^2 .* D2'*D2 + 2.*lambda .* D1'*D1;
Z = (E + P) \ Y;
复制代码
欢迎光临 Mathematica中文论坛-非官方 (http://ilovemathematica.com/)
Powered by Discuz! X3.2