comparison scripts/plot/gnuplot_drawnow.m @ 8356:d750feaefa8e

gnuplot_drawnow.m: Add support of properites to gp backend.
author Ben Abbott <bpabbott@mac.com>
date Fri, 14 Nov 2008 08:12:55 -0500
parents 1f429086565c
children c7e49bf03d42
comparison
equal deleted inserted replaced
8355:a8019b9644ca 8356:d750feaefa8e
32 if (nargin < 4) 32 if (nargin < 4)
33 mono = false; 33 mono = false;
34 endif 34 endif
35 35
36 if (nargin >= 3 && nargin <= 5) 36 if (nargin >= 3 && nargin <= 5)
37 ## Produce various output formats, or redirect gnuplot stream to a debug file
37 plot_stream = []; 38 plot_stream = [];
38 fid = []; 39 fid = [];
39 unwind_protect 40 unwind_protect
40 [plot_stream, enhanced] = open_gnuplot_stream (1, [], term, file); 41 plot_stream = open_gnuplot_stream (1, []);
42 enhanced = gnuplot_set_term (plot_stream (1), h, term, file);
41 __go_draw_figure__ (h, plot_stream, enhanced, mono); 43 __go_draw_figure__ (h, plot_stream, enhanced, mono);
42 if (nargin == 5) 44 if (nargin == 5)
43 fid = fopen (debug_file, "wb"); 45 fid = fopen (debug_file, "wb");
44 enhanced = init_plot_stream (fid, [], term, file); 46 enhanced = gnuplot_set_term (fid, h, term, file);
45 __go_draw_figure__ (h, fid, enhanced, mono); 47 __go_draw_figure__ (h, fid, enhanced, mono);
46 endif 48 endif
47 unwind_protect_cleanup 49 unwind_protect_cleanup
48 if (! isempty (plot_stream)) 50 if (! isempty (plot_stream))
49 pclose (plot_stream); 51 pclose (plot_stream);
51 if (! isempty (fid)) 53 if (! isempty (fid))
52 fclose (fid); 54 fclose (fid);
53 endif 55 endif
54 end_unwind_protect 56 end_unwind_protect
55 elseif (nargin == 1) 57 elseif (nargin == 1)
58 ## Graphics terminal for display
56 plot_stream = get (h, "__plot_stream__"); 59 plot_stream = get (h, "__plot_stream__");
57 if (isempty (plot_stream)) 60 if (isempty (plot_stream))
58 [plot_stream, enhanced] = open_gnuplot_stream (2, h); 61 plot_stream = open_gnuplot_stream (2, h);
59 set (h, "__enhanced__", enhanced); 62 endif
60 else 63 enhanced = gnuplot_set_term (plot_stream (1), h);
61 enhanced = get (h, "__enhanced__");
62 endif
63 __go_draw_figure__ (h, plot_stream (1), enhanced, mono); 64 __go_draw_figure__ (h, plot_stream (1), enhanced, mono);
64 fflush (plot_stream (1)); 65 fflush (plot_stream (1));
65 else 66 else
66 print_usage (); 67 print_usage ();
67 endif 68 endif
68 69
69 endfunction 70 endfunction
70 71
71 function [plot_stream, enhanced] = open_gnuplot_stream (npipes, h, varargin) 72 function [plot_stream, enhanced] = open_gnuplot_stream (npipes, h)
72
73 cmd = gnuplot_binary (); 73 cmd = gnuplot_binary ();
74
75 if (npipes > 1) 74 if (npipes > 1)
76 [plot_stream(1), plot_stream(2), pid] = popen2 (cmd); 75 [plot_stream(1), plot_stream(2), pid] = popen2 (cmd);
77 if (pid < 0) 76 if (pid < 0)
78 error ("drawnow: failed to open connection to gnuplot"); 77 error ("drawnow: failed to open connection to gnuplot");
79 endif 78 endif
81 plot_stream = popen (cmd, "w"); 80 plot_stream = popen (cmd, "w");
82 if (plot_stream < 0) 81 if (plot_stream < 0)
83 error ("drawnow: failed to open connection to gnuplot"); 82 error ("drawnow: failed to open connection to gnuplot");
84 endif 83 endif
85 endif 84 endif
86
87 if (! isempty (h)) 85 if (! isempty (h))
88 set (h, "__plot_stream__", plot_stream); 86 set (h, "__plot_stream__", plot_stream);
89 endif 87 endif
90 88 endfunction
91 enhanced = init_plot_stream (plot_stream (1), h, varargin{:}); 89
92 90 function [ enhanced ] = gnuplot_set_term (plot_stream, h, term, file)
93 endfunction 91 ## Generate the gnuplot "set terminal <term> ..." command. Include the subset
94 92 ## of properties "position", "units", "paperposition", "paperunits", "name",
95 function enhanced = init_plot_stream (plot_stream, h, term, file) 93 ## and "numbertitle". When "term" originates from print.m, it may include
94 ## gnuplot terminal options.
95 if (nargin == 2)
96 ## This supports the gnuplot backend
97 term = gnuplot_term ();
98 opts_str = "";
99 else
100 ## Get the one word terminal id and save the remaining as options to be passed
101 ## on to gnuplot. The terminal may respect the backend.
102 [term, opts_str] = gnuplot_trim_term (term);
103 endif
104
105 enhanced = gnuplot_is_enhanced_term (term);
106
107 ## Set the terminal
108 if (! isempty (term))
109
110 if (enhanced)
111 enh_str = "enhanced";
112 else
113 enh_str = "";
114 endif
115
116 if (! isempty (h) && isfigure (h))
117
118 ## Generate gnuoplot title string for backend plot windows
119 if (isbackend (term))
120 fig.numbertitle = get (h, "numbertitle");
121 fig.name = get (h, "name");
122 if (strcmpi (get (h, "numbertitle"), "on"))
123 title_str = sprintf ("Figure %d", h);
124 else
125 title_str = "";
126 end
127 if (! isempty (fig.name) && ! isempty (title_str))
128 title_str = sprintf ("%s: %s", title_str, fig.name);
129 elseif (! isempty (fig.name) && isempty (title_str))
130 title_str = fig.name;
131 end
132 if (! isempty (title_str))
133 title_str = sprintf ("title \"%s\"", title_str);
134 endif
135 else
136 title_str = "";
137 endif
138
139 if (isempty (strfind (opts_str, " size ")))
140 ## Convert position to units used by gnuplot.
141 if (isbackend (term))
142 ## Get figure size in pixels.
143 gnuplot_size = get_figsize (h);
144 else
145 ## Get size of the printed plot in inches.
146 gnuplot_size = get_canvassize (h);
147 ## If the output is a bitmap, convert to pixels.
148 if (isbitmap (term) || strcmpi (term, "svg"))
149 gnuplot_size = gnuplot_size * get (0, "screenpixelsperinch");
150 endif
151 endif
152 if (all (gnuplot_size > 0))
153 ## Set terminal size
154 terminals_with_size = {"emf", "gif", "jpeg", "latex", "pbm", ...
155 "pdf", "png", "postscript", "svg", "wxt", ...
156 "epslatex", "pstex", "pslatex"};
157 if (any (strncmpi (term, terminals_with_size, 3)))
158 size_str = sprintf("size %d,%d", gnuplot_size(1), gnuplot_size(2));
159 elseif (any (strncmpi (term, {"aqua", "fig"}, 3)))
160 ## Aqua and Fig also have size, but the format is different.
161 size_str = sprintf("size %d %d", gnuplot_size(1), gnuplot_size(2));
162 elseif (any (strncmpi (term, {"corel", "hpgl"}, 3)))
163 ## The size for corel and hpgl are goes at the end (implicit)
164 size_str = sprintf("%d %d",gnuplot_size(1), gnuplot_size(2));
165 elseif (any (strncmpi (term, {"dxf"}, 3)))
166 ## DXF uses autocad units
167 size_str = "";
168 else
169 size_str = "";
170 endif
171 else
172 size_str = "";
173 warning ("gnuplot_set_term: size is zero")
174 endif
175 else
176 ## A specified size take priority over the figure properies.
177 size_str = "";
178 endif
179 else
180 if isempty (h)
181 disp ("gnuplot_set_term: figure handle is empty")
182 elseif !isfigure(h)
183 disp ("gnuplot_set_term: not a figure handle")
184 endif
185 title_str = "";
186 size_str = "";
187 endif
188
189 ## Set the gnuplot terminal (type, enhanced?, title, & size)
190 if (! isempty (term))
191 term_str = sprintf ("set terminal %s", term);
192 if (! isempty (enh_str))
193 term_str = sprintf ("%s %s", term_str, enh_str);
194 end
195 if (! isempty (title_str))
196 term_str = sprintf ("%s %s", term_str, title_str);
197 end
198 if (! isempty (size_str))
199 ## size_str goes last to permit specification of canvas size
200 ## for terminals cdr/corel
201 term_str = sprintf ("%s %s", term_str, size_str);
202 end
203 if (nargin > 4 && ischar (opts_str))
204 ## Options must go last
205 term_str = sprintf ("%s %s", term_str, opts_str);
206 endif
207 fprintf (plot_stream, sprintf ("%s\n", term_str));
208 else
209 ## gnuplot will pick up the GNUTERM environment variable itself
210 ## so no need to set the terminal type if not also setting the
211 ## figure title, enhanced mode, or position.
212 end
213 endif
96 214
97 if (nargin == 4) 215 if (nargin == 4)
98 enhanced = enhanced_term (term);
99 if (! isempty (term))
100 if (enhanced)
101 fprintf (plot_stream, "set terminal %s enhanced;\n", term);
102 else
103 fprintf (plot_stream, "set terminal %s;\n", term);
104 endif
105 endif
106 if (! isempty (file)) 216 if (! isempty (file))
107 fprintf (plot_stream, "set output \"%s\";\n", file); 217 fprintf (plot_stream, "set output \"%s\";\n", file);
108 endif 218 endif
109 else 219 endif
110 220 endfunction
111 ## Guess the terminal type. 221
112 term = getenv ("GNUTERM"); 222 function term = gnuplot_term ()
113 if (isempty (term)) 223 term = getenv ("GNUTERM");
114 if (! isempty (getenv ("DISPLAY"))) 224 ## If not specified, guess the terminal type.
115 term = "x11"; 225 if (isempty (term))
116 elseif (! isunix ()) 226 if (ismac ())
117 term = "windows"; 227 term = "aqua";
118 else 228 elseif (! isunix ())
119 ## This should really be checking for os x before setting 229 term = "windows";
120 ## the terminal type to aqua, but nobody will notice because 230 else
121 ## every other unix will be using x11 and windows will be 231 term = "x11";
122 ## using windows. Those diehards still running octave from 232 endif
123 ## a linux console know how to set the GNUTERM variable. 233 endif
124 term = "aqua"; 234 endfunction
125 endif 235
126 endif 236 function [term, opts] = gnuplot_trim_term (string)
127 237 ## Extract the terminal type and terminal options (from print.m)
128 enhanced = enhanced_term (term); 238 string = deblank (string);
129 if (enhanced) 239 n = strfind (string, ' ');
130 enh_str = "enhanced"; 240 if (isempty (n))
131 else 241 term = string;
132 enh_str = ""; 242 opts = "";
133 endif 243 else
134 244 term = string(1:(n-1));
135 ## If no 'h' (why not?) then open the terminal as Figure 0. 245 opts = string((n+1):end);
136 if (isempty (h)) 246 endif
137 h = 0; 247 endfunction
138 endif 248
139 249 function have_enhanced = gnuplot_is_enhanced_term (term)
140 if (strcmp (term, "x11"))
141 fprintf (plot_stream, "set terminal x11 %s title \"Figure %d\"\n",
142 enh_str, h);
143 elseif (strcmp (term, "aqua"))
144 ## Aqua doesn't understand the 'title' option despite what the
145 ## gnuplot 4.2 documentation says.
146 fprintf (plot_stream, "set terminal aqua %d %s\n", h, enh_str);
147 elseif (strcmp (term, "wxt"))
148 fprintf (plot_stream, "set terminal wxt %s title \"Figure %d\"\n",
149 enh_str, h);
150
151 elseif (enhanced)
152 fprintf (plot_stream, "set terminal %s %s\n", term, enh_str);
153 endif
154 ## gnuplot will pick up the GNUTERM environment variable itself
155 ## so no need to set the terminal type if not also setting the
156 ## figure title or enhanced mode.
157
158 endif
159
160 endfunction
161
162 function have_enhanced = enhanced_term (term)
163 persistent enhanced_terminals; 250 persistent enhanced_terminals;
164
165 if (isempty (enhanced_terminals)) 251 if (isempty (enhanced_terminals))
166 ## Don't include pstex, pslatex or epslatex here as the TeX commands 252 ## Don't include pstex, pslatex or epslatex here as the TeX commands
167 ## should not be interpreted in that case. 253 ## should not be interpreted in that case.
168 if (compare_versions (__gnuplot_version__ (), "4.0", ">")) 254 enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ...
169 enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ... 255 "windows", "wxt", "svg", "postscript", "x11", "pdf"};
170 "windows", "wxt", "svg", "postscript", "x11", "pdf"}; 256 endif
171 else 257 if (nargin < 1)
172 enhanced_terminals = {"x11", "postscript"}; 258 ## Determine the default gnuplot terminal.
173 endif 259 term = gnuplot_term ();
174 endif 260 endif
175
176 term = tolower (term);
177
178 have_enhanced = false; 261 have_enhanced = false;
179 for i = 1 : length (enhanced_terminals) 262 for n = 1 : length (enhanced_terminals)
180 t = enhanced_terminals{i}; 263 t = enhanced_terminals{n};
181 if (strncmp (term, t, min (length (term), length (t)))) 264 if (strncmp (term, t, min (length (term), length (t))))
182 have_enhanced = true; 265 have_enhanced = true;
183 break; 266 break;
184 endif 267 endif
185 endfor 268 endfor
186 endfunction 269 endfunction
270
271 function ret = isbackend (term)
272 if (nargin == 0)
273 term = gnuplot_term ();
274 endif
275 ret = any (strcmpi ({"aqua", "wxt", "x11", "windows", "pm"}, term));
276 endfunction
277
278 function ret = isbitmap (term)
279 if (nargin == 0)
280 term = gnuplot_term ();
281 endif
282 ret = any (strcmpi ({"png", "jpeg", "gif", "pbm"}, term));
283 endfunction
284
285 function [ fig_size ] = get_figsize (h)
286 ## Determine the size of the figure in pixels
287 possize = get (h, "position")(3:4);
288 units = get (h, "units");
289 t.inches = 1;
290 t.centimeters = 2.54;
291 t.pixels = get (0, "screenpixelsperinch");
292 ## gnuplot treats pixels/points the same
293 t.points = t.pixels;
294 t.normalized = get (0, "screensize")(3:4) / t.pixels;
295 fig_size = possize * (t.pixels / t.(units));
296 if (prod (fig_size) > 1e8)
297 warning ("gnuplot_drawnow: figure size is excessive. Reducing to 1024x768.")
298 fig_size = [1024, 768];
299 position = get (h, "position");
300 set (h, "position", [position(1:2), fig_size], "units", "pixels");
301 endif
302 endfunction
303
304 function [plotsize] = get_canvassize (h)
305 ## Returns the intended size of the plot on the page in inches.
306 ## "canvas size" is a gnuplot term. Gnuplot doesn't explicity plot to
307 ## an area/position on a page. Instead it plots to a "canvas" of a
308 ## explicit or implicit size.
309 t.points = get (0, "screenpixelsperinch");
310 t.centimeters = 2.54;
311 t.inches = 1.00;
312 papersize = get_papersize (h);
313 paperunits = get (h, "paperunits");
314 paperposition = get (h, "paperposition") / t.(paperunits);
315 if (strcmpi (paperunits, "normalized"))
316 plotsize = papersize .* paperposition(3:4);
317 else
318 plotsize = paperposition(3:4);
319 endif
320 endfunction
321
322 function [ papersize ] = get_papersize (h)
323 ## Returns the papersize in inches
324 persistent papertypes papersizes
325 if (isempty (papertypes))
326 papertypes = {"usletter", "uslegal", ...
327 "a0", "a1", "a2", "a3", "a4", "a5", ...
328 "b0", "b1", "b2", "b3", "b4", "b5", ...
329 "arch-a", "arch-b", "arch-c", "arch-d", "arch-e", ...
330 "a", "b", "c", "d", "e", ...
331 "tabloid", "<custom>"};
332 papersizes = [ 8.500, 11.000
333 8.500, 14.000
334 33.135, 46.847
335 23.404, 33.135
336 16.548, 23.404
337 11.694, 16.528
338 8.268, 11.693
339 5.847, 8.264
340 40.543, 57.366
341 28.683, 40.503
342 20.252, 28.683
343 14.342, 20.252
344 10.126, 14.342
345 7.171, 10.126
346 9.000, 12.000
347 12.000, 18.000
348 18.000, 24.000
349 24.000, 36.000
350 36.000, 48.000
351 8.500, 11.000
352 11.000, 17.000
353 17.000, 22.000
354 22.000, 34.000
355 34.000, 44.000
356 11.000, 17.000
357 8.500, 11.000];
358 ## <custom> has a page size since we're not doing any checking here.
359 papersizes = round (1000 * papersizes);
360 endif
361
362 paperunits = get (h, "paperunits");
363 if (strcmpi (paperunits, "normalized"))
364 papertype = get (h, "papertype");
365 n = find (strcmpi (papertypes, papertype));
366 papersize = 0.001 * papersizes(n, :);
367 paperunits = "inches";
368 else
369 t.points = 72;
370 t.centimeters = 2.54;
371 t.inches = 1.00;
372 ## FIXME - this papersize/type administration should be done at a lower level.
373 if (strcmpi (get (h, "papertype"), "<custom>"))
374 ## if the type is custom but the size is a standard, then set the standard type.
375 papersize = get (h "papersize");
376 papersize = papersize * t.(paperunits);
377 n = find (all ((ones ([size(papersizes, 1), 1]) * round(1000*papersize) - papersizes) == 0, 2));
378 if (! isempty (n))
379 set (h, "papertype", papertypes{n});
380 endif
381 else
382 papertype = get (h, "papertype");
383 n = find (strcmpi (papertypes, papertype));
384 papersize = papersizes(n,:) * 0.001;
385 set (h, "papersize", papersize * t.(paperunits));
386 endif
387 endif
388 endfunction