-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotmatrix_v2.m
179 lines (157 loc) · 5.82 KB
/
plotmatrix_v2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function [h,ax,BigAx,patches,pax] = plotmatrix_v2(varargin)
%PLOTMATRIX Scatter plot matrix.
% PLOTMATRIX(X,Y) scatter plots the columns of X against the columns
% of Y. If X is P-by-M and Y is P-by-N, PLOTMATRIX will produce a
% N-by-M matrix of axes. PLOTMATRIX(Y) is the same as PLOTMATRIX(Y,Y)
% except that the diagonal will be replaced by HIST(Y(:,i)).
%
% PLOTMATRIX(...,'LineSpec') uses the given line specification in the
% string 'LineSpec'; '.' is the default (see PLOT for possibilities).
%
% PLOTMATRIX(AX,...) uses AX as the BigAx instead of GCA.
%
% [H,AX,BigAx,P,PAx] = PLOTMATRIX(...) returns a matrix of handles
% to the objects created in H, a matrix of handles to the individual
% subaxes in AX, a handle to big (invisible) axes that frame the
% subaxes in BigAx, a matrix of handles for the histogram plots in
% P, and a matrix of handles for invisible axes that control the
% histogram axes scales in PAx. BigAx is left as the CurrentAxes so
% that a subsequent TITLE, XLABEL, or YLABEL will be centered with
% respect to the matrix of axes.
%
% Example:
% x = randn(50,3); y = x*[-1 2 1;2 0 1;1 -2 3;]';
% plotmatrix(y)
% Clay M. Thompson 10-3-94
% Copyright 1984-2005 The MathWorks, Inc.
% $Revision: 1.19.4.7 $ $Date: 2005/10/28 15:54:39 $
% Parse possible Axes input
[cax,args,nargs] = axescheck(varargin{:});
error(nargchk(1,3,nargs,'struct'));
nin = nargs;
sym = '.'; % Default scatter plot symbol.
dohist = 0;
if ischar(args{nin}),
sym = args{nin};
[l,c,m,msg] = colstyle(sym); %#ok
if ~isempty(msg), error(msg); end %#ok
nin = nin - 1;
end
if nin==1, % plotmatrix(y)
rows = size(args{1},2); cols = rows;
x = args{1}; y = args{1};
dohist = 1;
elseif nin==2, % plotmatrix(x,y)
rows = size(args{2},2); cols = size(args{1},2);
x = args{1}; y = args{2};
else
error('MATLAB:plotmatrix:InvalidLineSpec',...
'Invalid marker specification. Type ''help plot''.');
end
for i=1:cols
[f_IS(i,:),xi_IS(i,:)] = ksdensity(x(:,i));
end
% Don't plot anything if either x or y is empty
patches = [];
pax = [];
if isempty(rows) || isempty(cols),
if nargout>0, h = []; ax = []; BigAx = []; end
return
end
if ndims(x)>2 || ndims(y)>2,
error(id('InvalidXYMatrices'),'X and Y must be 2-D.')
end
if size(x,1)~=size(y,1) || size(x,3)~=size(y,3),
error(id('XYSizeMismatch'),'X and Y must have the same number of rows and pages.');
end
% Create/find BigAx and make it invisible
BigAx = newplot(cax);
fig = ancestor(BigAx,'figure');
hold_state = ishold(BigAx);
set(BigAx,'Visible','off','color','none')
if any(sym=='.'),
units = get(BigAx,'units');
set(BigAx,'units','pixels');
pos = get(BigAx,'Position');
set(BigAx,'units',units);
markersize = max(1,min(15,round(15*min(pos(3:4))/max(1,size(x,1))/max(rows,cols))));
else
markersize = get(0,'defaultlinemarkersize');
end
% Create and plot into axes
ax = zeros(rows,cols);
pos = get(BigAx,'Position');
width = pos(3)/cols;
height = pos(4)/rows;
space = .02; % 2 percent space between axes
pos(1:2) = pos(1:2) + space*[width height];
m = size(y,1);
k = size(y,3);
xlim = zeros([rows cols 2]);
ylim = zeros([rows cols 2]);
BigAxHV = get(BigAx,'HandleVisibility');
BigAxParent = get(BigAx,'Parent');
for i=rows:-1:1,
for j=cols:-1:1,
axPos = [pos(1)+(j-1)*width pos(2)+(rows-i)*height ...
width*(1-space) height*(1-space)];
findax = findobj(fig,'Type','axes','Position',axPos);
if isempty(findax),
ax(i,j) = axes('Position',axPos,'HandleVisibility',BigAxHV,'parent',BigAxParent);
set(ax(i,j),'visible','on');
else
ax(i,j) = findax(1);
end
hh(i,j,:) = plot(reshape(x(:,j,:),[m k]), ...
reshape(y(:,i,:),[m k]),sym,'parent',ax(i,j))';
set(hh(i,j,:),'markersize',markersize);
set(ax(i,j),'xlimmode','auto','ylimmode','auto','xgrid','off','ygrid','off')
xlim(i,j,:) = get(ax(i,j),'xlim');
ylim(i,j,:) = get(ax(i,j),'ylim');
end
end
xlimmin = min(xlim(:,:,1),[],1); xlimmax = max(xlim(:,:,2),[],1);
ylimmin = min(ylim(:,:,1),[],2); ylimmax = max(ylim(:,:,2),[],2);
% Try to be smart about axes limits and labels. Set all the limits of a
% row or column to be the same and inset the tick marks by 10 percent.
inset = .15;
for i=1:rows,
set(ax(i,1),'ylim',[ylimmin(i,1) ylimmax(i,1)])
dy = diff(get(ax(i,1),'ylim'))*inset;
set(ax(i,:),'ylim',[ylimmin(i,1)-dy ylimmax(i,1)+dy])
end
dx = zeros(1,cols);
for j=1:cols,
set(ax(1,j),'xlim',[xlimmin(1,j) xlimmax(1,j)])
dx(j) = diff(get(ax(1,j),'xlim'))*inset;
set(ax(:,j),'xlim',[xlimmin(1,j)-dx(j) xlimmax(1,j)+dx(j)])
end
set(ax(1:rows-1,:),'xticklabel','')
set(ax(:,2:cols),'yticklabel','')
set(BigAx,'XTick',get(ax(rows,1),'xtick'),'YTick',get(ax(rows,1),'ytick'), ...
'userdata',ax,'tag','PlotMatrixBigAx')
if dohist, % Put a histogram on the diagonal for plotmatrix(y) case
for i=rows:-1:1,
histax = axes('Position',get(ax(i,i),'Position'),'HandleVisibility',BigAxHV,'parent',BigAxParent);
% [nn,xx] = hist(reshape(y(:,i,:),[m k]));
% patches(i,:) = bar(histax,xx,nn,'hist');
plot(xi_IS(i,:),f_IS(i,:));%plot(xi_IS(i,:),f_IS(i,:),'k',xi_IS_1,f_IS_1,'r');
set(histax,'xtick',[],'ytick',[],'xgrid','off','ygrid','off');
set(histax,'xlim',[xlimmin(1,i)-dx(i) xlimmax(1,i)+dx(i)])
pax(i) = histax; % ax handles for histograms
end
patches = patches';
end
% Make BigAx the CurrentAxes
set(fig,'CurrentAx',BigAx)
if ~hold_state,
set(fig,'NextPlot','replace')
end
% Also set Title and X/YLabel visibility to on and strings to empty
set([get(BigAx,'Title'); get(BigAx,'XLabel'); get(BigAx,'YLabel')], ...
'String','','Visible','on')
if nargout~=0,
h = hh;
end
function str=id(str)
str = ['MATLAB:plotmatrix:' str];