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