comparison scripts/plot/plotmatrix.m @ 8127:86568be36992

Add the plotmatrix function
author David Bateman <dbateman@free.fr>
date Mon, 22 Sep 2008 13:34:07 -0400
parents
children 73d6b71788c0
comparison
equal deleted inserted replaced
8126:a012580f0d2f 8127:86568be36992
1 ## Copyright (C) 2008 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} plotmatrix (@var{x}, @var{y})
21 ## @deftypefnx {Function File} {} plotmatrix (@var{x})
22 ## @deftypefnx {Function File} {} plotmatrix (@dots{}, @var{style})
23 ## @deftypefnx {Function File} {} plotmatrix (@var{h}, @dots{})
24 ## @deftypefnx {Function File} {[@var{h}, @var{ax}, @var{bigax}, @var{p}, @var{pax}] =} plotmatrix (@dots{})
25 ## Scatter plot of the columns of one matrix against another. Given the
26 ## arguments @var{x} and @var{y}, that have a matching number of rows,
27 ## @code{plotmatrix} plots a set of axes corresponding to
28 ##
29 ## @example
30 ## plot (@var{x} (:, i), @var{y} (:, j)
31 ## @end example
32 ##
33 ## Given a single argument @var{x}, then this is equivalent to
34 ##
35 ## @example
36 ## plotmatrix (@var{x}, @var{x})
37 ## @end example
38 ##
39 ## @noindent
40 ## except that the diagonal of the set of axes will be replaced with the
41 ## histogram @code{hist (@var{x} (:, i))}.
42 ##
43 ## The marker to use can be changed with the @var{style} argument, that is a
44 ## string defining a marker in the same manner as the @code{plot}
45 ## command. If a leading axes handle @var{h} is passed to
46 ## @code{plotmatrix}, then this axis will be used for the plot.
47 ##
48 ## The optional return value @var{h} provides handles to the individual
49 ## graphics objects in the scatter plots, whereas @var{ax} returns the
50 ## handles to the scatter plot axis objects. @var{bigax} is a hidden
51 ## axis object that surrounds the other axes, such that the commands
52 ## @code{xlabel}, @code{title}, etc, will be associated with this hidden
53 ## axis. Finally @var{p} returns the graphics objects associated with
54 ## the histogram and @var{pax} the corresponding axes objects.
55 ##
56 ## @example
57 ## @group
58 ## plotmatrix (randn (100, 3), 'g+')
59 ## @end group
60 ## @end example
61 ##
62 ## @end deftypefn
63
64 function [h, ax, bigax, p, pax] = plotmatrix (varargin)
65
66 [bigax2, varargin, nargin] = __plt_get_axis_arg__ ("plotmatrix", varargin{:});
67
68 if (nargin > 3 || nargin < 1)
69 print_usage ();
70 else
71 oldh = gca ();
72 unwind_protect
73 axes (bigax2);
74 newplot ();
75 [h2, ax2, p2, pax2, need_usage] = __plotmatrix__ (bigax2, varargin{:});
76 if (need_usage)
77 print_usage ();
78 endif
79 if (nargout > 0)
80 h = h2;
81 ax = ax2;
82 bigax = bigax2;
83 p = p2;
84 pax = pax2;
85 endif
86 axes (bigax2);
87 ctext = text (0, 0, "", "visible", "off",
88 "handlevisibility", "off", "xliminclude", "off",
89 "yliminclude", "off", "zliminclude", "off",
90 "deletefcn", {@plotmatrixdelete, [ax2; pax2]});
91 set (bigax2, "visible", "off");
92 unwind_protect_cleanup
93 axes (oldh);
94 end_unwind_protect
95 endif
96 endfunction
97
98 %!demo
99 %! plotmatrix (randn (100, 3), 'g+')
100
101 function plotmatrixdelete (h, d, ax)
102 for i = 1 : numel (ax)
103 hc = ax(i);
104 if (ishandle (hc) && strcmp (get (hc, "type"), "axes") &&
105 strcmp (get (hc, "beingdeleted"), "off"))
106 parent = get (hc, "parent");
107 ## If the parent is invalid or being deleted, then do nothing
108 if (ishandle (parent) && strcmp (get (parent, "beingdeleted"), "off"))
109 delete (hc);
110 endif
111 endif
112 endfor
113 endfunction
114
115 function [h, ax, p, pax, need_usage] = __plotmatrix__ (bigax, varargin)
116 need_usage = false;
117 have_line_spec = false;
118 have_hist = false;
119 parent = get (bigax, "parent");
120 for i = 1 : nargin - 1
121 arg = varargin{i};
122 if (ischar (arg) || iscell (arg))
123 [linespec, valid] = __pltopt__ ("plotmatrix", varargin{i}, false);
124 if (valid)
125 have_line_spec = true;
126 linespec = varargin(i);
127 varargin(i) = [];
128 nargin = nargin - 1;
129 break;
130 else
131 need_usage = true;
132 returm;
133 endif
134 endif
135 endfor
136
137 if (nargin == 2)
138 X = varargin{1};
139 Y = X;
140 have_hist = true;
141 elseif (nargin == 3)
142 X = varargin{1};
143 Y = varargin{2};
144 else
145 need_usage = true;
146 returm;
147 endif
148
149 if (rows(X) != rows(Y))
150 error ("plotmatrix: dimension mismatch in the arguments");
151 endif
152
153 [dummy, m] = size (X);
154 [dummy, n] = size (Y);
155
156 h = [];
157 ax = [];
158 p = [];
159 pax = [];
160
161 xsize = 0.9 / m;
162 ysize = 0.9 / n;
163 xoff = 0.05;
164 yoff = 0.05;
165 border = [0.130, 0.110, 0.225, 0.185] .* [xsize, ysize, xsize, ysize];
166 border(3:4) = - border(3:4) - border(1:2);
167
168 for i = 1 : n
169 for j = 1 : m
170 pos = [xsize * (j - 1) + xoff, ysize * (n - i) + yoff, xsize, ysize];
171 tmp = axes ("outerposition", pos, "position", pos + border,
172 "parent", parent);
173 if (i == j && have_hist)
174 pax = [pax ; tmp];
175 [nn, xx] = hist (X(:, i));
176 tmp = bar (xx, nn, 1.0);
177 p = [p; tmp];
178 else
179 ax = [ax ; tmp];
180 if (have_line_spec)
181 tmp = plot (X (:, i), Y (:, j), linespec);
182 else
183 tmp = plot (X (:, i), Y (:, j), ".");
184 endif
185 h = [h ; tmp];
186 endif
187 endfor
188 endfor
189 endfunction