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