-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplotmatrix2.m
More file actions
194 lines (174 loc) · 6.32 KB
/
plotmatrix2.m
File metadata and controls
194 lines (174 loc) · 6.32 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
function [h,ax,BigAx,hhist,pax] = plotmatrix2(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 HISTOGRAM(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 vector of handles for the histogram plots in
% P, and a vector 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)
% Copyright 1984-2015 The MathWorks, Inc.
% Parse possible Axes input
[cax,args,nargs] = axescheck(varargin{:});
if nargs < 1
error(message('MATLAB:narginchk:notEnoughInputs'));
elseif nargs > 3
error(message('MATLAB:narginchk:tooManyInputs'));
end
nin = nargs;
sym = '.'; % Default scatter plot symbol.
dohist = 0;
if ischar(args{nin}),
sym = args{nin};
[~,~,~,msg] = colstyle(sym);
if ~isempty(msg), error(msg); end
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(message('MATLAB:plotmatrix:InvalidLineSpec'));
end
% Don't plot anything if either x or y is empty
hhist = gobjects(0);
pax = gobjects(0);
if isempty(rows) || isempty(cols),
if nargout>0, h = gobjects(0); ax = gobjects(0); BigAx = gobjects(0); end
return
end
if ~ismatrix(x) || ~ismatrix(y)
error(message('MATLAB:plotmatrix:InvalidXYMatrices'))
end
if size(x,1)~=size(y,1) || size(x,3)~=size(y,3),
error(message('MATLAB:plotmatrix:XYSizeMismatch'));
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 = gobjects(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');
paxes = findobj(fig,'Type','axes','tag','PlotMatrixScatterAx');
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 = findaxpos(paxes, 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))';
hh(i,j,:) = histogram2(reshape(x(:,j,:),[m k]),reshape(y(:,i,:),[m k]),50,'DisplayStyle','tile','Normalization','pdf','ShowEmptyBins','on','EdgeColor','none');
% 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 = 0;%.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')
set(ax,'tag','PlotMatrixScatterAx');
if dohist, % Put a histogram on the diagonal for plotmatrix(y) case
paxes = findobj(fig,'Type','axes','tag','PlotMatrixHistAx');
pax = gobjects(1, rows);
for i=rows:-1:1,
axPos = get(ax(i,i),'Position');
findax = findaxpos(paxes, axPos);
if isempty(findax),
histax = axes('Position',axPos,'HandleVisibility',BigAxHV,'parent',BigAxParent);
set(histax,'visible','on');
else
histax = findax(1);
end
hhist(i) = histogram(histax,y(:,i,:),50,'Normalization','pdf');
set(histax,'xtick',[],'ytick',[],'xgrid','off','ygrid','off');
set(histax,'xlim',[xlimmin(1,i)-dx(i) xlimmax(1,i)+dx(i)])
set(histax,'tag','PlotMatrixHistAx');
pax(i) = histax; % ax handles for histograms
end
end
% Make BigAx the CurrentAxes
set(fig,'CurrentAx',BigAx)
if ~hold_state,
set(fig,'NextPlot','replacechildren')
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 findax = findaxpos(ax, axpos)
tol = eps;
findax = [];
for i = 1:length(ax)
axipos = get(ax(i),'Position');
diffpos = axipos - axpos;
if (max(max(abs(diffpos))) < tol)
findax = ax(i);
break;
end
end