6405
|
1 ## Copyright (C) 2005 John W. Eaton |
|
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 2, or (at your option) |
|
8 ## 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, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} __go_draw_axes__ (@var{axis_obj}, @var{plot_stream}) |
|
22 ## Display the axes @var{axis_obj} on @var{plot_stream}. |
|
23 ## @end deftypefn |
|
24 |
|
25 ## Author: jwe |
|
26 |
|
27 function __go_draw_axes__ (h, plot_stream) |
|
28 |
|
29 if (nargin == 2) |
|
30 |
|
31 axis_obj = get (h); |
|
32 |
|
33 parent_figure_obj = get (axis_obj.parent); |
|
34 |
6413
|
35 persistent have_newer_gnuplot ... |
|
36 = compare_versions (__gnuplot_version__ (), "4.0", ">"); |
6405
|
37 |
|
38 ## Set axis properties here? |
|
39 |
|
40 if (! isempty (axis_obj.outerposition)) |
|
41 pos = axis_obj.outerposition; |
|
42 fprintf (plot_stream, "set origin %g, %g;\n", pos(1), pos(2)); |
|
43 fprintf (plot_stream, "set size %g, %g;\n", pos(3), pos(4)); |
|
44 endif |
|
45 |
|
46 if (! isempty (axis_obj.position)) |
|
47 pos = axis_obj.position; |
|
48 fprintf (plot_stream, "set origin %g, %g;\n", pos(1), pos(2)); |
|
49 fprintf (plot_stream, "set size %g, %g;\n", pos(3), pos(4)); |
|
50 endif |
|
51 |
6758
|
52 if (strcmpi (axis_obj.dataaspectratiomode, "manual")) |
6405
|
53 r = axis_obj.dataaspectratio; |
|
54 fprintf (plot_stream, "set size ratio %g;\n", -r(2)/r(1)); |
|
55 else |
|
56 fputs (plot_stream, "set size noratio;\n"); |
|
57 endif |
|
58 |
6778
|
59 fputs (plot_stream, "unset label;\n"); |
|
60 |
6405
|
61 if (! isempty (axis_obj.title)) |
|
62 t = get (axis_obj.title); |
|
63 if (isempty (t.string)) |
|
64 fputs (plot_stream, "unset title;\n"); |
|
65 else |
|
66 fprintf (plot_stream, "set title \"%s\";\n", |
|
67 undo_string_escapes (t.string)); |
|
68 endif |
|
69 endif |
|
70 |
|
71 if (! isempty (axis_obj.xlabel)) |
|
72 t = get (axis_obj.xlabel); |
6737
|
73 angle = t.rotation; |
6405
|
74 if (isempty (t.string)) |
|
75 fputs (plot_stream, "unset xlabel;\n"); |
|
76 else |
6738
|
77 fprintf (plot_stream, "set xlabel \"%s\"", |
|
78 undo_string_escapes (t.string)); |
|
79 if (have_newer_gnuplot) |
|
80 ## Rotation of xlabel not yet support by gnuplot as of 4.2, but |
|
81 ## there is no message about it. |
|
82 fprintf (plot_stream, " rotate by %f", angle); |
|
83 endif |
|
84 fputs (plot_stream, ";\n"); |
6405
|
85 endif |
|
86 endif |
|
87 |
|
88 if (! isempty (axis_obj.ylabel)) |
|
89 t = get (axis_obj.ylabel); |
6737
|
90 angle = t.rotation; |
6405
|
91 if (isempty (t.string)) |
|
92 fputs (plot_stream, "unset ylabel;\n"); |
|
93 else |
6738
|
94 fprintf (plot_stream, "set ylabel \"%s\"", |
|
95 undo_string_escapes (t.string)); |
|
96 if (have_newer_gnuplot) |
6766
|
97 fprintf (plot_stream, " rotate by %f;\n", angle); |
6738
|
98 endif |
|
99 fputs (plot_stream, ";\n"); |
6405
|
100 endif |
|
101 endif |
|
102 |
|
103 if (! isempty (axis_obj.zlabel)) |
|
104 t = get (axis_obj.zlabel); |
6737
|
105 angle = t.rotation; |
6405
|
106 if (isempty (t.string)) |
|
107 fputs (plot_stream, "unset zlabel;\n"); |
|
108 else |
6738
|
109 fprintf (plot_stream, "set zlabel \"%s\"", |
|
110 undo_string_escapes (t.string)); |
|
111 if (have_newer_gnuplot) |
|
112 ## Rotation of zlabel not yet support by gnuplot as of 4.2, but |
|
113 ## there is no message about it. |
|
114 fprintf (plot_stream, " rotate by %f;\n", angle); |
|
115 endif |
|
116 fputs (plot_stream, ";\n"); |
6405
|
117 endif |
|
118 endif |
|
119 |
6758
|
120 if (strcmpi (axis_obj.xgrid, "on")) |
6405
|
121 fputs (plot_stream, "set grid xtics;\n"); |
|
122 else |
|
123 fputs (plot_stream, "set grid noxtics;\n"); |
|
124 endif |
|
125 |
6758
|
126 if (strcmpi (axis_obj.ygrid, "on")) |
6405
|
127 fputs (plot_stream, "set grid ytics;\n"); |
|
128 else |
|
129 fputs (plot_stream, "set grid noytics;\n"); |
|
130 endif |
|
131 |
6758
|
132 if (strcmpi (axis_obj.zgrid, "on")) |
6405
|
133 fputs (plot_stream, "set grid ztics;\n"); |
|
134 else |
|
135 fputs (plot_stream, "set grid ztics;\n"); |
|
136 endif |
|
137 |
6758
|
138 if (strcmpi (axis_obj.xminorgrid, "on")) |
6405
|
139 fputs (plot_stream, "set mxtics 5;\n"); |
|
140 fputs (plot_stream, "set grid mxtics;\n"); |
|
141 else |
|
142 fputs (plot_stream, "set grid nomxtics;\n"); |
|
143 endif |
|
144 |
6758
|
145 if (strcmpi (axis_obj.yminorgrid, "on")) |
6405
|
146 fputs (plot_stream, "set mytics 5;\n"); |
|
147 fputs (plot_stream, "set grid mytics;\n"); |
|
148 else |
|
149 fputs (plot_stream, "set grid nomytics;\n"); |
|
150 endif |
|
151 |
6758
|
152 if (strcmpi (axis_obj.zminorgrid, "on")) |
6405
|
153 fputs (plot_stream, "set mztics 5;\n"); |
|
154 fputs (plot_stream, "set grid mztics;\n"); |
|
155 else |
|
156 fputs (plot_stream, "set grid nomztics;\n"); |
|
157 endif |
|
158 |
6745
|
159 do_tics (axis_obj, plot_stream); |
6405
|
160 |
6758
|
161 xlogscale = strcmpi (axis_obj.xscale, "log"); |
6405
|
162 if (xlogscale) |
|
163 fputs (plot_stream, "set logscale x;\n"); |
|
164 else |
|
165 fputs (plot_stream, "unset logscale x;\n"); |
|
166 endif |
|
167 |
6758
|
168 ylogscale = strcmpi (axis_obj.yscale, "log"); |
6405
|
169 if (ylogscale) |
|
170 fputs (plot_stream, "set logscale y;\n"); |
|
171 else |
|
172 fputs (plot_stream, "unset logscale y;\n"); |
|
173 endif |
|
174 |
6758
|
175 zlogscale = strcmpi (axis_obj.zscale, "log"); |
6405
|
176 if (zlogscale) |
|
177 fputs (plot_stream, "set logscale z;\n"); |
|
178 else |
|
179 fputs (plot_stream, "unset logscale z;\n"); |
|
180 endif |
|
181 |
6758
|
182 xautoscale = strcmpi (axis_obj.xlimmode, "auto"); |
|
183 yautoscale = strcmpi (axis_obj.ylimmode, "auto"); |
|
184 zautoscale = strcmpi (axis_obj.zlimmode, "auto"); |
6405
|
185 |
|
186 kids = axis_obj.children; |
|
187 |
|
188 nd = 0; |
|
189 data_idx = 0; |
|
190 data = cell (); |
6464
|
191 is_image_data = []; |
6405
|
192 |
6453
|
193 xminp = yminp = zminp = realmax (); |
|
194 xmax = ymax = zmax = -realmax (); |
|
195 xmin = ymin = zmin = realmax (); |
6405
|
196 |
|
197 [view_cmd, view_fcn, view_zoom] = image_viewer (); |
|
198 use_gnuplot_for_images = (ischar (view_fcn) |
|
199 && strcmp (view_fcn, "gnuplot_internal")); |
|
200 |
|
201 ximg_data = {}; |
|
202 ximg_data_idx = 0; |
|
203 |
|
204 for i = 1:length (kids) |
|
205 |
|
206 obj = get (kids(i)); |
|
207 |
|
208 switch (obj.type) |
|
209 case "image" |
|
210 ## FIXME - Is there a better way to determine if the plot |
|
211 ## command should be "plot" or "splot"????? Could have images |
|
212 ## projected into 3D so there is really no reason to limit |
|
213 ## this. |
|
214 if (nd == 0) |
|
215 nd = 2; |
|
216 endif |
|
217 |
|
218 img_data = obj.cdata; |
|
219 img_colormap = parent_figure_obj.colormap; |
|
220 img_xdata = obj.xdata; |
|
221 img_ydata = obj.ydata; |
|
222 |
|
223 if (use_gnuplot_for_images) |
|
224 |
|
225 data_idx++; |
6464
|
226 is_image_data(data_idx) = true; |
6405
|
227 |
|
228 [y_dim, x_dim] = size (img_data(:,:,1)); |
|
229 if (x_dim > 1) |
|
230 dx = abs (img_xdata(2)-img_xdata(1))/(x_dim-1); |
|
231 else |
|
232 dx = 1; |
|
233 endif |
|
234 if (y_dim > 1) |
|
235 dy = abs (img_ydata(2)-img_ydata(1))/(y_dim-1); |
|
236 else |
|
237 dy = 1; |
|
238 endif |
|
239 x_origin = min (img_xdata); |
|
240 y_origin = min (img_ydata); |
|
241 |
|
242 if (nd == 2) |
|
243 if (xautoscale) |
|
244 xmin = min (xmin, min (img_xdata) - dx/2); |
|
245 xmax = max (xmax, max (img_xdata) + dx/2); |
|
246 xminp = min (xminp, min (img_xdata((img_xdata - dx/2)>0)) - dx/2); |
|
247 endif |
|
248 if (yautoscale) |
|
249 ymin = min (ymin, min (img_ydata) - dy/2); |
|
250 ymax = max (ymax, max (img_ydata) + dy/2); |
|
251 yminp = min (yminp, min (img_ydata((img_ydata - dy/2)>0)) - dy/2); |
|
252 endif |
|
253 else |
|
254 ## Can have images in 3D, but the image routines don't seem |
|
255 ## to have a means of arbitrary projection. |
|
256 endif |
|
257 |
|
258 if (ndims (img_data) == 3) |
6464
|
259 data{data_idx} = permute (img_data, [3, 1, 2])(:); |
6405
|
260 format = "1:2:3"; |
|
261 imagetype = "rgbimage"; |
|
262 else |
6464
|
263 data{data_idx} = img_data(:); |
6405
|
264 format = "1"; |
|
265 imagetype = "image"; |
6464
|
266 |
|
267 palette_size = rows (img_colormap); |
|
268 fprintf (plot_stream, |
|
269 "set palette positive color model RGB maxcolors %i;\n", |
|
270 palette_size); |
|
271 fprintf (plot_stream, |
|
272 "set palette file \"-\" binary record=%d using 1:2:3:4;\n", |
|
273 palette_size); |
|
274 fwrite (plot_stream, [1:palette_size; img_colormap'], "float32"); |
6405
|
275 endif |
|
276 |
6579
|
277 titlespec{data_idx} = "title \"\""; |
6420
|
278 usingclause{data_idx} = sprintf ("binary array=%dx%d scan=yx origin=(%g,%g) dx=%g dy=%g using %s", |
6405
|
279 x_dim, y_dim, x_origin, y_origin, dx, dy, format); |
|
280 withclause{data_idx} = sprintf ("with %s", imagetype); |
|
281 |
|
282 else |
|
283 ximg_data{++ximg_data_idx} = img_data; |
|
284 endif |
|
285 |
|
286 case "line" |
|
287 data_idx++; |
6464
|
288 is_image_data(data_idx) = false; |
6405
|
289 if (isempty (obj.keylabel)) |
|
290 titlespec{data_idx} = "title \"\""; |
|
291 else |
|
292 titlespec{data_idx} = strcat ("title \"", obj.keylabel, "\""); |
|
293 endif |
6465
|
294 [style, typ, with] = do_linestyle_command (obj, data_idx, plot_stream); |
6405
|
295 usingclause{data_idx} = ""; |
6425
|
296 if (have_newer_gnuplot || isnan (typ)) |
|
297 withclause{data_idx} = sprintf ("with %s linestyle %d", |
|
298 style, data_idx); |
|
299 else |
|
300 withclause{data_idx} = sprintf ("with %s linetype %d", |
|
301 style, typ); |
|
302 endif |
6405
|
303 parametric(i) = true; |
|
304 if (! isempty (obj.zdata)) |
|
305 nd = 3; |
|
306 xdat = obj.xdata(:); |
|
307 ydat = obj.ydata(:); |
|
308 zdat = obj.zdata(:); |
|
309 if (xautoscale) |
6453
|
310 [xmin, xmax, xminp] = get_data_limits (xmin, xmax, xminp, xdat); |
6405
|
311 endif |
|
312 if (yautoscale) |
6453
|
313 [ymin, ymax, yminp] = get_data_limits (ymin, ymax, yminp, ydat); |
6405
|
314 endif |
|
315 if (zautoscale) |
6453
|
316 [zmin, zmax, zminp] = get_data_limits (zmin, zmax, zminp, zdat); |
6405
|
317 endif |
|
318 data{data_idx} = [xdat, ydat, zdat]'; |
|
319 usingclause{data_idx} = "using ($1):($2):($3)"; |
|
320 fputs (plot_stream, "set parametric;\n"); |
|
321 fputs (plot_stream, "unset hidden3d;\n"); |
|
322 fputs (plot_stream, "set style data lines;\n"); |
|
323 fputs (plot_stream, "set surface;\n"); |
|
324 fputs (plot_stream, "unset contour;\n"); |
|
325 else |
|
326 nd = 2; |
|
327 xdat = obj.xdata(:); |
|
328 ydat = obj.ydata(:); |
|
329 ldat = obj.ldata; |
|
330 yerr = xerr = false; |
|
331 if (! isempty (ldat)) |
|
332 yerr = true; |
|
333 ldat = ldat(:); |
|
334 endif |
|
335 udat = obj.udata; |
|
336 if (! isempty (udat)) |
|
337 udat = udat(:); |
|
338 endif |
|
339 xldat = obj.xldata; |
|
340 if (! isempty (xldat)) |
|
341 xerr = true; |
|
342 xldat = xldat(:); |
|
343 endif |
|
344 xudat = obj.xudata; |
|
345 if (! isempty (xudat)) |
|
346 xudat = xudat(:); |
|
347 endif |
|
348 if (yerr) |
|
349 ylo = ydat-ldat; |
|
350 yhi = ydat+udat; |
|
351 if (yautoscale) |
|
352 ty = [ydat; ylo; yhi]; |
6453
|
353 [ymin, ymax, yminp] = get_data_limits (ymin, ymax, yminp, ty); |
6405
|
354 endif |
|
355 if (xerr) |
|
356 xlo = xdat-xldat; |
|
357 xhi = xdat+xudat; |
|
358 if (xautoscale) |
|
359 tx = [xdat; xlo; xhi]; |
6453
|
360 [xmin, xmax, xminp] = get_data_limits (xmin, xmax, xminp, tx); |
6405
|
361 endif |
|
362 data{data_idx} = [xdat, ydat, xlo, xhi, ylo, yhi]'; |
|
363 usingclause{data_idx} = "using ($1):($2):($3):($4):($5):($6)"; |
|
364 withclause{data_idx} = sprintf ("with xyerrorbars linestyle %d", |
|
365 data_idx); |
|
366 else |
6736
|
367 ## Obtain the limits based on the exact x values. |
6405
|
368 if (xautoscale) |
6453
|
369 [xmin, xmax, xminp] = get_data_limits (xmin, xmax, |
6736
|
370 xminp, xdat); |
6405
|
371 endif |
|
372 data{data_idx} = [xdat, ydat, ylo, yhi]'; |
|
373 usingclause{data_idx} = "using ($1):($2):($3):($4)"; |
|
374 withclause{data_idx} = sprintf ("with yerrorbars linestyle %d", |
|
375 data_idx); |
|
376 endif |
|
377 elseif (xerr) |
|
378 xlo = xdat-xldat; |
|
379 xhi = xdat+xudat; |
|
380 if (xautoscale) |
|
381 tx = [xdat; xlo; xhi]; |
6453
|
382 [xmin, xmax, xminp] = get_data_limits (xmin, xmax, xminp, tx); |
6405
|
383 endif |
|
384 if (yautoscale) |
6453
|
385 [ymin, ymax, yminp] = get_data_limits (ymin, ymax, |
|
386 yminp, ydat, ty); |
6405
|
387 endif |
|
388 data{data_idx} = [xdat, ydat, xlo, xhi]'; |
|
389 usingclause{data_idx} = "using ($1):($2):($3):($4)"; |
|
390 withclause{data_idx} = sprintf ("with xerrorbars linestyle %d", |
|
391 data_idx); |
|
392 else |
|
393 if (xautoscale) |
6453
|
394 [xmin, xmax, xminp] = get_data_limits (xmin, xmax, xminp, xdat); |
6405
|
395 endif |
|
396 if (yautoscale) |
6453
|
397 [ymin, ymax, yminp] = get_data_limits (ymin, ymax, yminp, ydat); |
6405
|
398 endif |
|
399 data{data_idx} = [xdat, ydat]'; |
|
400 usingclause{data_idx} = "using ($1):($2)"; |
|
401 endif |
|
402 endif |
6465
|
403 if (! (have_newer_gnuplot || isempty (with))) |
|
404 if (isempty (withclause{data_idx})) |
|
405 withclause{data_idx} = sprintf("with %s", with); |
|
406 else |
|
407 withclause{data_idx} = sprintf("%s %s", withclause{data_idx}, with); |
|
408 endif |
|
409 endif |
6405
|
410 |
|
411 case "surface" |
|
412 data_idx++; |
6464
|
413 is_image_data(data_idx) = false; |
6465
|
414 [style, typ, with] = do_linestyle_command (obj, data_idx, plot_stream); |
6405
|
415 if (isempty (obj.keylabel)) |
|
416 titlespec{data_idx} = "title \"\""; |
|
417 else |
|
418 titlespec{data_idx} = strcat ("title \"", obj.keylabel, "\""); |
|
419 endif |
|
420 usingclause{data_idx} = ""; |
6425
|
421 if (have_newer_gnuplot || isnan (typ)) |
|
422 withclause{data_idx} = sprintf ("with %s linestyle %d", |
|
423 style, data_idx); |
|
424 else |
6465
|
425 withclause{data_idx} = sprintf ("with %s linetype %d %s", |
|
426 style, typ, with); |
6425
|
427 endif |
6405
|
428 parametric(i) = false; |
|
429 nd = 3; |
|
430 xdat = obj.xdata; |
|
431 ydat = obj.ydata; |
|
432 zdat = obj.zdata; |
|
433 if (xautoscale) |
|
434 tx = xdat(:); |
6453
|
435 [xmin, xmax, xminp] = get_data_limits (xmin, xmax, xminp, tx); |
6405
|
436 endif |
|
437 if (yautoscale) |
|
438 ty = ydat(:); |
6453
|
439 [ymin, ymax, yminp] = get_data_limits (ymin, ymax, yminp, ty); |
6405
|
440 endif |
|
441 if (zautoscale) |
|
442 tz = zdat(:); |
6453
|
443 [zmin, zmax, zminp] = get_data_limits (zmin, zmax, zminp, tz); |
6405
|
444 endif |
|
445 err = false; |
|
446 if (isvector (xdat) && isvector (ydat) && ismatrix (zdat)) |
|
447 if (rows (zdat) == length (ydat) && columns (zdat) == length (xdat)) |
|
448 [xdat, ydat] = meshgrid (xdat, ydat); |
|
449 else |
|
450 err = true; |
|
451 endif |
|
452 elseif (ismatrix (xdat) && ismatrix (ydat) && ismatrix (zdat)) |
|
453 if (! (size_equal (xdat, ydat) && size_equal (xdat, zdat))) |
|
454 err = true; |
|
455 endif |
|
456 else |
|
457 err = true; |
|
458 endif |
|
459 if (err) |
|
460 error ("__go_draw_axes__: invalid grid data"); |
|
461 endif |
|
462 xlen = columns (zdat); |
|
463 ylen = rows (zdat); |
|
464 if (xlen == columns (xdat) && xlen == columns (ydat) |
|
465 && ylen == rows (xdat) && ylen == rows (ydat)) |
|
466 len = 3 * xlen; |
|
467 zz = zeros (ylen, len); |
|
468 k = 1; |
|
469 for kk = 1:3:len |
|
470 zz(:,kk) = xdat(:,k); |
|
471 zz(:,kk+1) = ydat(:,k); |
|
472 zz(:,kk+2) = zdat(:,k); |
|
473 k++; |
|
474 endfor |
|
475 data{data_idx} = zz; |
|
476 endif |
|
477 usingclause{data_idx} = "using ($1):($2):($3)"; |
|
478 withclause{data_idx} = "with line palette"; |
|
479 |
|
480 fputs (plot_stream, "unset parametric;\n"); |
|
481 fputs (plot_stream, "set hidden3d;\n"); |
|
482 fputs (plot_stream, "set style data lines;\n"); |
|
483 fputs (plot_stream, "set surface;\n"); |
|
484 fputs (plot_stream, "unset contour;\n"); |
6788
|
485 |
|
486 if (have_newer_gnuplot) |
|
487 surf_colormap = parent_figure_obj.colormap; |
|
488 palette_size = rows (surf_colormap); |
|
489 fprintf (plot_stream, |
|
490 "set palette positive color model RGB maxcolors %i;\n", |
|
491 palette_size); |
|
492 fprintf (plot_stream, |
|
493 "set palette file \"-\" binary record=%d using 1:2:3:4;\n", |
|
494 palette_size); |
|
495 fwrite (plot_stream, [1:palette_size; surf_colormap'], "float32"); |
|
496 else |
|
497 fputs (plot_stream, "set palette defined (0 \"dark-blue\", 1 \"blue\", 2 \"cyan\", 3 \"yellow\", 4 \"red\" , 5 \"dark-red\");\n"); |
|
498 endif |
6405
|
499 fputs (plot_stream, "unset colorbox;\n"); |
|
500 |
|
501 case "text" |
|
502 lpos = obj.position; |
|
503 label = obj.string; |
|
504 halign = obj.horizontalalignment; |
6724
|
505 angle = obj.rotation; |
6752
|
506 units = obj.units; |
6758
|
507 if (strcmpi (units, "normalized")) |
6752
|
508 units = "graph"; |
|
509 else |
|
510 units = ""; |
|
511 endif |
|
512 |
6405
|
513 if (nd == 3) |
6724
|
514 fprintf (plot_stream, |
6752
|
515 "set label \"%s\" at %s %g,%g,%g %s rotate by %f;\n", |
|
516 undo_string_escapes (label), units, |
6724
|
517 lpos(1), lpos(2), lpos(3), halign, angle); |
6405
|
518 else |
6724
|
519 fprintf (plot_stream, |
6752
|
520 "set label \"%s\" at %s %g,%g %s rotate by %f;\n", |
|
521 undo_string_escapes (label), units, |
6724
|
522 lpos(1), lpos(2), halign, angle); |
6405
|
523 endif |
|
524 |
|
525 otherwise |
|
526 error ("__go_draw_axes__: unknown object class, %s", |
|
527 obj.type); |
|
528 endswitch |
|
529 |
|
530 endfor |
|
531 |
|
532 have_data = ! isempty (data); |
|
533 |
|
534 if (xautoscale && have_data) |
|
535 xlim = get_axis_limits (xmin, xmax, xminp, xlogscale); |
|
536 set (h, "xlim", xlim, "xlimmode", "auto"); |
|
537 else |
|
538 xlim = axis_obj.xlim; |
|
539 endif |
6758
|
540 if (strcmpi (axis_obj.xdir, "reverse")) |
6405
|
541 xdir = "reverse"; |
|
542 else |
|
543 xdir = "noreverse"; |
|
544 endif |
|
545 fprintf (plot_stream, "set xrange [%g:%g] %s;\n", xlim, xdir); |
|
546 |
|
547 if (yautoscale && have_data) |
|
548 ylim = get_axis_limits (ymin, ymax, yminp, ylogscale); |
|
549 set (h, "ylim", ylim, "ylimmode", "auto"); |
|
550 else |
|
551 ylim = axis_obj.ylim; |
|
552 endif |
6758
|
553 if (strcmpi (axis_obj.ydir, "reverse")) |
6405
|
554 ydir = "reverse"; |
|
555 else |
|
556 ydir = "noreverse"; |
|
557 endif |
|
558 fprintf (plot_stream, "set yrange [%g:%g] %s;\n", ylim, ydir); |
|
559 |
|
560 if (nd == 3) |
|
561 if (zautoscale && have_data) |
|
562 zlim = get_axis_limits (zmin, zmax, zminp, zlogscale); |
|
563 set (h, "zlim", zlim, "zlimmode", "auto"); |
|
564 else |
|
565 zlim = axis_obj.zlim; |
|
566 endif |
6758
|
567 if (strcmpi (axis_obj.zdir, "reverse")) |
6405
|
568 zdir = "reverse"; |
|
569 else |
|
570 zdir = "noreverse"; |
|
571 endif |
|
572 fprintf (plot_stream, "set zrange [%g:%g] %s;\n", zlim, zdir); |
|
573 endif |
|
574 |
6758
|
575 if (strcmpi (axis_obj.box, "on")) |
6405
|
576 if (nd == 3) |
|
577 fputs (plot_stream, "set border 4095;\n"); |
|
578 else |
|
579 fputs (plot_stream, "set border 431;\n"); |
|
580 endif |
|
581 else |
|
582 if (nd == 3) |
|
583 fputs (plot_stream, "set border 895;\n"); |
|
584 else |
|
585 fputs (plot_stream, "set border 3;\n"); |
|
586 fputs (plot_stream, "set xtics nomirror; set ytics nomirror;\n"); |
|
587 endif |
|
588 endif |
|
589 |
6758
|
590 if (strcmpi (axis_obj.key, "on")) |
|
591 if (strcmpi (axis_obj.keybox, "on")) |
6405
|
592 box = "box"; |
|
593 else |
|
594 box = "nobox"; |
|
595 endif |
|
596 inout = "inside"; |
|
597 switch (axis_obj.keypos) |
|
598 case -1 |
|
599 pos = "right bottom"; |
|
600 inout = "outside"; |
|
601 case 1 |
|
602 pos = "right top"; |
|
603 case 2 |
|
604 pos = "left top"; |
|
605 case 3 |
|
606 pos = "left bottom"; |
|
607 case 4 |
|
608 pos = "right bottom"; |
|
609 otherwise |
|
610 pos = ""; |
|
611 endswitch |
|
612 if (! have_newer_gnuplot) |
|
613 inout = ""; |
|
614 endif |
|
615 fprintf (plot_stream, "set key %s %s %s;\n", inout, pos, box); |
|
616 else |
|
617 fputs (plot_stream, "unset key;\n"); |
|
618 endif |
|
619 |
|
620 fputs (plot_stream, "set style data lines;\n"); |
|
621 |
|
622 if (! use_gnuplot_for_images) |
|
623 for i = 1:ximg_data_idx |
|
624 view_fcn (xlim, ylim, ximg_data{i}, view_zoom, view_cmd); |
|
625 endfor |
|
626 endif |
|
627 |
|
628 if (have_data) |
|
629 |
|
630 if (nd == 2) |
|
631 plot_cmd = "plot"; |
|
632 else |
|
633 plot_cmd = "splot"; |
|
634 rot_x = 90 - axis_obj.view(2); |
|
635 rot_z = axis_obj.view(1); |
|
636 while (rot_z < 0) |
|
637 rot_z += 360; |
|
638 endwhile |
6461
|
639 fputs (plot_stream, "set ticslevel 0;\n"); |
6405
|
640 fprintf (plot_stream, "set view %g, %g;\n", rot_x, rot_z); |
|
641 endif |
6611
|
642 fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd, |
6464
|
643 usingclause{1}, titlespec{1}, withclause{1}); |
6405
|
644 for i = 2:data_idx |
6611
|
645 fprintf (plot_stream, ", \"-\" %s %s %s \\\n", |
6464
|
646 usingclause{i}, titlespec{i}, withclause{i}); |
6405
|
647 endfor |
|
648 fputs (plot_stream, ";\n"); |
|
649 for i = 1:data_idx |
6464
|
650 if (is_image_data(i)) |
|
651 fwrite (plot_stream, data{i}, "float32"); |
|
652 else |
6510
|
653 __gnuplot_write_data__ (plot_stream, data{i}, nd, parametric(i)); |
6405
|
654 endif |
|
655 endfor |
6431
|
656 else |
|
657 fputs (plot_stream, "plot \"-\";\nInf Inf\ne\n"); |
6405
|
658 endif |
|
659 |
|
660 fflush (plot_stream); |
|
661 |
|
662 else |
|
663 print_usage (); |
|
664 endif |
|
665 |
|
666 endfunction |
|
667 |
6453
|
668 function [xmin, xmax, xminp] = get_data_limits (xmin, xmax, xminp, xdat, tx) |
|
669 xdat = xdat(! isinf (xdat)); |
|
670 xmin = min (xmin, min (xdat)); |
|
671 xmax = max (xmax, max (xdat)); |
|
672 if (nargin == 5) |
|
673 tx = tx(! isinf (xdat) & tx > 0); |
|
674 xminp = min (xminp, min (tx)); |
|
675 else |
|
676 xminp = min (xminp, min (xdat(xdat>0))); |
|
677 endif |
|
678 endfunction |
|
679 |
6405
|
680 ## Attempt to make "nice" limits from the actual max and min of the |
|
681 ## data. For log plots, we will also use the smallest strictly positive |
|
682 ## value. |
|
683 |
|
684 function lim = get_axis_limits (min_val, max_val, min_pos, logscale) |
|
685 |
|
686 if (logscale) |
|
687 if (isinf (min_pos)) |
|
688 warning ("axis: logscale with no positive values to plot"); |
|
689 endif |
6527
|
690 if (min_val <= 0) |
6405
|
691 min_val = min_pos; |
6527
|
692 if (max_val <= 0) |
6405
|
693 max_val = min_pos; |
|
694 endif |
6527
|
695 warning ("axis: omitting nonpositive data in log plot"); |
6405
|
696 endif |
6512
|
697 ## FIXME -- maybe this test should also be relative? |
|
698 if (abs (min_val - max_val) < sqrt (eps)) |
|
699 min_val *= 0.9; |
|
700 max_val *= 1.1; |
6405
|
701 endif |
|
702 min_val = 10 ^ floor (log10 (min_val)); |
|
703 max_val = 10 ^ ceil (log10 (max_val)); |
|
704 else |
|
705 if (min_val == 0 && max_val == 0) |
|
706 min_val = -1; |
|
707 max_val = 1; |
6512
|
708 ## FIXME -- maybe this test should also be relative? |
|
709 elseif (abs (min_val - max_val) < sqrt (eps)) |
|
710 min_val -= 0.1 * abs (min_val); |
|
711 max_val += 0.1 * abs (max_val); |
6405
|
712 endif |
|
713 ## FIXME -- to do a better job, we should consider the tic spacing. |
|
714 scale = 10 ^ floor (log10 (max_val - min_val) - 1); |
|
715 min_val = scale * floor (min_val / scale); |
|
716 max_val = scale * ceil (max_val / scale); |
|
717 endif |
|
718 |
|
719 lim = [min_val, max_val]; |
|
720 |
|
721 endfunction |
|
722 |
6465
|
723 function [style, typ, with] = do_linestyle_command (obj, idx, plot_stream) |
6405
|
724 |
6413
|
725 persistent have_newer_gnuplot ... |
|
726 = compare_versions (__gnuplot_version__ (), "4.0", ">"); |
6405
|
727 |
|
728 if (have_newer_gnuplot) |
|
729 fprintf (plot_stream, "set style line %d default;\n", idx); |
|
730 endif |
|
731 fprintf (plot_stream, "set style line %d", idx); |
|
732 |
|
733 found_style = false; |
6425
|
734 typ = NaN; |
6465
|
735 with = ""; |
6405
|
736 |
6425
|
737 if (isfield (obj, "color")) |
6405
|
738 color = obj.color; |
|
739 if (isnumeric (color)) |
6425
|
740 if (have_newer_gnuplot) |
|
741 fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"", |
|
742 round (255*color)); |
|
743 else |
|
744 if (isequal (color, [0,0,0])) |
|
745 typ = -1; |
|
746 elseif (isequal (color, [1,0,0])) |
|
747 typ = 1; |
|
748 elseif (isequal (color, [0,1,0])) |
|
749 typ = 2; |
|
750 elseif (isequal (color, [0,0,1])) |
|
751 typ = 3; |
|
752 elseif (isequal (color, [1,0,1])) |
|
753 typ = 4; |
|
754 elseif (isequal (color, [0,1,1])) |
|
755 typ = 5; |
|
756 elseif (isequal (color, [1,1,1])) |
|
757 typ = 6; |
|
758 elseif (isequal (color, [1,1,0])) |
|
759 typ = 7; |
|
760 else |
|
761 typ = 2; |
|
762 endif |
|
763 endif |
6405
|
764 endif |
|
765 found_style = true; |
|
766 endif |
|
767 |
|
768 if (isfield (obj, "linestyle")) |
|
769 switch (obj.linestyle) |
|
770 case "-" |
6415
|
771 lt = "1"; |
6405
|
772 case "--" |
6415
|
773 lt = "2"; |
6405
|
774 case ":" |
6415
|
775 lt = "3"; |
6405
|
776 case "-." |
6415
|
777 lt = "6"; |
6405
|
778 case "none" |
|
779 lt = ""; |
|
780 otherwise |
|
781 lt = ""; |
|
782 endswitch |
6415
|
783 if (! isempty (lt)) |
|
784 fprintf (plot_stream, " linetype %s", lt); |
|
785 found_style = true; |
|
786 endif |
6405
|
787 else |
|
788 lt = ""; |
|
789 endif |
|
790 |
|
791 if (isfield (obj, "linewidth")) |
6465
|
792 if (have_newer_gnuplot) |
|
793 fprintf (plot_stream, " linewidth %f", obj.linewidth); |
|
794 else |
|
795 with = sprintf ("%s lw %f", with, obj.linewidth); |
|
796 endif |
6405
|
797 found_style = true; |
|
798 endif |
|
799 |
|
800 if (isfield (obj, "marker")) |
|
801 switch (obj.marker) |
|
802 case "+" |
|
803 pt = "1"; |
|
804 case "o" |
6413
|
805 pt = "6"; |
6405
|
806 case "*" |
|
807 pt = "3"; |
|
808 case "." |
6413
|
809 pt = "7"; |
6405
|
810 case "x" |
|
811 pt = "2"; |
|
812 case {"square", "s"} |
|
813 pt = "5"; |
|
814 case {"diamond", "d"} |
|
815 pt = "13"; |
|
816 case "^" |
|
817 pt = "9"; |
|
818 case "v" |
|
819 pt = "11"; |
|
820 case ">" |
|
821 pt = "8"; |
|
822 case "<" |
|
823 pt = "10"; |
|
824 case {"pentagram", "p"} |
|
825 pt = "4"; |
|
826 case {"hexagram", "h"} |
6413
|
827 pt = "12"; |
6405
|
828 case "none" |
|
829 pt = ""; |
|
830 otherwise |
|
831 pt = ""; |
|
832 endswitch |
|
833 if (! isempty (pt)) |
6465
|
834 if (have_newer_gnuplot) |
|
835 fprintf (plot_stream, " pointtype %s", pt); |
|
836 else |
|
837 with = sprintf ("%s pt %s", with, pt); |
|
838 endif |
6405
|
839 found_style = true; |
|
840 endif |
|
841 else |
|
842 pt = ""; |
|
843 endif |
|
844 |
|
845 style = "lines"; |
|
846 if (isempty (lt)) |
|
847 if (! isempty (pt)) |
|
848 style = "points"; |
|
849 endif |
|
850 elseif (! isempty (pt)) |
|
851 style = "linespoints"; |
|
852 endif |
|
853 |
6465
|
854 if (isfield (obj, "markersize")) |
|
855 if (have_newer_gnuplot) |
|
856 fprintf (plot_stream, " pointsize %f", obj.markersize); |
|
857 else |
6758
|
858 if (! strcmpi (style, "lines")) |
6465
|
859 with = sprintf ("%s ps %f", with, obj.markersize); |
|
860 endif |
|
861 endif |
|
862 found_style = true; |
|
863 endif |
|
864 |
6405
|
865 if (have_newer_gnuplot && ! found_style) |
|
866 fputs (plot_stream, " default"); |
|
867 endif |
|
868 |
|
869 fputs (plot_stream, ";\n"); |
|
870 |
|
871 endfunction |
6510
|
872 |
|
873 function __gnuplot_write_data__ (plot_stream, data, nd, parametric) |
|
874 |
|
875 ## DATA is already transposed. |
|
876 |
|
877 ## FIXME -- this may need to be converted to C++ for speed. |
|
878 |
6605
|
879 ## Convert NA elements to normal NaN values because fprintf writes |
|
880 ## "NA" and that confuses gnuplot. |
|
881 idx = find (isna (data)); |
|
882 if (any (idx)) |
|
883 data(idx) = NaN; |
|
884 endif |
|
885 |
6510
|
886 if (nd == 2) |
|
887 nan_elts = find (sum (isnan (data))); |
|
888 fmt = strcat (repmat ("%g ", 1, rows (data)), "\n"); |
|
889 if (isempty (nan_elts)) |
|
890 fprintf (plot_stream, fmt, data); |
|
891 else |
|
892 n = columns (data); |
|
893 have_nans = true; |
|
894 num_nan_elts = numel (nan_elts); |
|
895 k = 1; |
|
896 for i = 1:n |
|
897 if (have_nans && i == nan_elts(k)) |
|
898 fputs (plot_stream, "\n"); |
|
899 have_nans = ++k <= num_nan_elts; |
|
900 else |
|
901 fprintf (plot_stream, fmt, data(:,i)); |
|
902 endif |
|
903 endfor |
|
904 endif |
|
905 else |
|
906 ## FIXME -- handle NaNs here too? |
|
907 if (parametric) |
|
908 fprintf (plot_stream, "%g %g %g\n", data); |
|
909 else |
|
910 nc = columns (data); |
|
911 for j = 1:3:nc |
|
912 fprintf (plot_stream, "%g %g %g\n", data(:,j:j+2)'); |
|
913 fputs (plot_stream, "\n"); |
|
914 endfor |
|
915 endif |
|
916 endif |
|
917 fputs (plot_stream, "e\n"); |
|
918 |
|
919 endfunction |
6745
|
920 |
|
921 function do_tics (obj, plot_stream) |
|
922 do_tics_1 (obj.xtickmode, obj.xtick, obj.xticklabelmode, obj.xticklabel, |
|
923 "x", plot_stream); |
|
924 do_tics_1 (obj.ytickmode, obj.ytick, obj.yticklabelmode, obj.yticklabel, |
|
925 "y", plot_stream); |
|
926 do_tics_1 (obj.ztickmode, obj.ztick, obj.zticklabelmode, obj.zticklabel, |
|
927 "z", plot_stream); |
|
928 endfunction |
|
929 |
|
930 function do_tics_1 (ticmode, tics, labelmode, labels, ax, plot_stream) |
6758
|
931 if (strcmpi (ticmode, "manual")) |
6745
|
932 if (isempty (tics)) |
|
933 fprintf (plot_stream, "unset %stics;\n", ax); |
6758
|
934 elseif (strcmpi (labelmode, "manual") && ! isempty (labels)) |
6751
|
935 if (ischar (labels)) |
|
936 labels = cellstr (labels); |
|
937 endif |
6745
|
938 if (iscellstr (labels)) |
6751
|
939 k = 1; |
|
940 ntics = numel (tics); |
|
941 nlabels = numel (labels); |
6745
|
942 fprintf (plot_stream, "set format %s \"%%s\";\n", ax); |
|
943 fprintf (plot_stream, "set %stics (", ax); |
|
944 for i = 1:ntics |
|
945 fprintf (plot_stream, " \"%s\" %g", labels(k++), tics(i)) |
|
946 if (i < ntics) |
|
947 fputs (plot_stream, ", "); |
|
948 endif |
|
949 if (k > nlabels) |
|
950 k = 1; |
|
951 endif |
|
952 endfor |
|
953 fputs (plot_stream, ");\n"); |
|
954 else |
|
955 error ("unsupported type of ticklabel"); |
|
956 endif |
|
957 else |
|
958 fprintf (plot_stream, "set %stics (", ax); |
6748
|
959 fprintf (plot_stream, " %g,", tics(1:end-1)); |
|
960 fprintf (plot_stream, " %g);\n", tics(end)); |
6745
|
961 endif |
|
962 else |
|
963 fprintf (plot_stream, "set %stics;\n", ax); |
|
964 endif |
|
965 endfunction |