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