Mercurial > octave-nkf
annotate doc/interpreter/plotimages.m @ 20357:03c88436ad8c
Correctly generate figure 15.7 of the Manual.
* plot.txi: Improve the wording discussing the standalone example of 15.7.
* plotimages.m (set_graphics_toolkit): New subfunction to choose a graphics
toolkit with a preference for OpenGL based toolkits.
* plotimages.m (hide_output): Use figure (..., "visible", "off") to stop
window flicker during generation of images.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 10 Jul 2015 21:10:42 -0700 |
parents | fc3056b9fac0 |
children | b439ccc9a162 |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2007-2015 John W. Eaton and David Bateman |
7018 | 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 3 of the License, or (at | |
8 ## your option) 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, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
19 function plotimages (d, nm, typ) |
20357
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
20 set_graphics_toolkit (); |
9088
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
21 set_print_size (); |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
22 hide_output (); |
20351
d8992a16643c
doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
23 outfile = fullfile (d, [nm "." typ]); |
7257 | 24 if (strcmp (typ, "png")) |
25 set (0, "defaulttextfontname", "*"); | |
26 endif | |
9914
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
27 if (strcmp (typ, "eps")) |
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
28 d_typ = "-depsc2"; |
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
29 else |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
30 d_typ = ["-d", typ]; |
9914
e76d8c767584
Switch to color output for PostScript documentation images
Rik <rdrider0-list@yahoo.com>
parents:
9912
diff
changeset
|
31 endif |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
9298
diff
changeset
|
32 |
20351
d8992a16643c
doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
33 if (strcmp (typ , "txt")) |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
34 image_as_txt (d, nm); |
7256 | 35 elseif (strcmp (nm, "plot")) |
6888 | 36 x = -10:0.1:10; |
37 plot (x, sin (x)); | |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
38 xlabel ("x"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
39 ylabel ("sin (x)"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
40 title ("Simple 2-D Plot"); |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
41 print (outfile, d_typ); |
6888 | 42 elseif (strcmp (nm, "hist")) |
19825
b254a2e0859c
plotimages.m: Produce the same hist plot for documentation each time (bug #44263)
Rik <rik@octave.org>
parents:
19751
diff
changeset
|
43 randn ("state", 1); |
6888 | 44 hist (randn (10000, 1), 30); |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
45 xlabel ("Value"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
46 ylabel ("Count"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
47 title ("Histogram of 10,000 normally distributed random numbers"); |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
48 print (outfile, d_typ); |
6888 | 49 elseif (strcmp (nm, "errorbar")) |
11033
d9c8916bb9dd
Untabify a few remaining .m scripts.
Rik <octave@nomad.inbox5.com>
parents:
9914
diff
changeset
|
50 rand ("state", 2); |
6888 | 51 x = 0:0.1:10; |
52 y = sin (x); | |
9298
20ee7f9cc904
Correct bugs in plot images produced for documentation
Rik <rdrider0-list@yahoo.com>
parents:
9245
diff
changeset
|
53 yl = 0.1 .* rand (size (x)); |
20ee7f9cc904
Correct bugs in plot images produced for documentation
Rik <rdrider0-list@yahoo.com>
parents:
9245
diff
changeset
|
54 yu = 0.1 .* rand (size (x)); |
20ee7f9cc904
Correct bugs in plot images produced for documentation
Rik <rdrider0-list@yahoo.com>
parents:
9245
diff
changeset
|
55 errorbar (x, sin (x), yl, yu); |
11033
d9c8916bb9dd
Untabify a few remaining .m scripts.
Rik <octave@nomad.inbox5.com>
parents:
9914
diff
changeset
|
56 axis ([0, 10, -1.1, 1.1]); |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
57 xlabel ("x"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
58 ylabel ("sin (x)"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
59 title ("Errorbar plot of sin (x)"); |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
60 print (outfile, d_typ); |
6888 | 61 elseif (strcmp (nm, "polar")) |
62 polar (0:0.1:10*pi, 0:0.1:10*pi); | |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
63 set (gca, "rtick", [0:10:40]); |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
64 title ("Example polar plot from 0 to 10*pi"); |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
65 print (outfile, d_typ); |
6888 | 66 elseif (strcmp (nm, "mesh")) |
67 tx = ty = linspace (-8, 8, 41)'; | |
68 [xx, yy] = meshgrid (tx, ty); | |
69 r = sqrt (xx .^ 2 + yy .^ 2) + eps; | |
70 tz = sin (r) ./ r; | |
71 mesh (tx, ty, tz); | |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
72 xlabel ("tx"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
73 ylabel ("ty"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
74 zlabel ("tz"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
75 title ("3-D Sombrero plot"); |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
76 print (outfile, d_typ); |
6888 | 77 elseif (strcmp (nm, "plot3")) |
78 t = 0:0.1:10*pi; | |
79 r = linspace (0, 1, numel (t)); | |
80 z = linspace (0, 1, numel (t)); | |
20351
d8992a16643c
doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
81 plot3 (r.*sin (t), r.*cos (t), z); |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
82 xlabel ("r.*sin (t)"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
83 ylabel ("r.*cos (t)"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
84 zlabel ("z"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
85 title ("plot3 display of 3-D helix"); |
20342
c3c1fb44f9b5
eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents:
19825
diff
changeset
|
86 print (outfile, d_typ); |
7189 | 87 elseif (strcmp (nm, "extended")) |
88 x = 0:0.01:3; | |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
89 plot (x, erf (x)); |
7189 | 90 hold on; |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
91 plot (x, x, "r"); |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
92 axis ([0, 3, 0, 1]); |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
93 if (strcmp (typ, "pdf")) |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
94 text (0.65, 0.6175, ['$\leftarrow x = {2/\sqrt{\pi}' ... |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
95 '{\int_{0}^{x}}e^{-t^2} dt} = 0.6175$']); |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
96 else |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
97 text (0.65, 0.6175, ['\leftarrow x = {2/\surd\pi {\fontsize{16}' ... |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
98 '\int_{\fontsize{8}0}^{\fontsize{8}x}} e^{-t^2} dt} = 0.6175']); |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
99 endif |
17166
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
100 xlabel ("x"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
101 ylabel ("erf (x)"); |
0e34c475c956
plotimages.m: Add axis labels and titles to plots in Manual.
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
16994
diff
changeset
|
102 title ("erf (x) with text annotation"); |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
103 if (strcmp (typ, "pdf")) |
20356
fc3056b9fac0
write figure to specified output directory
John W. Eaton <jwe@octave.org>
parents:
20351
diff
changeset
|
104 print (outfile, "-dpdflatexstandalone"); |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
105 system (["pdflatex " nm]); |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
106 delete ([nm ".aux"], [nm "-inc.pdf"], [nm ".log"], [nm ".tex"]); |
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
107 else |
20356
fc3056b9fac0
write figure to specified output directory
John W. Eaton <jwe@octave.org>
parents:
20351
diff
changeset
|
108 print (outfile, d_typ); |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
109 endif |
6888 | 110 else |
111 error ("unrecognized plot requested"); | |
112 endif | |
9088
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
113 hide_output (); |
6888 | 114 endfunction |
115 | |
20357
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
116 ## Choose a toolkit with a preference for OpenGL renderers |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
117 function set_graphics_toolkit () |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
118 avl_tk = available_graphics_toolkits (); |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
119 if (any (strcmp ("qt", avl_tk))) |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
120 graphics_toolkit ("qt"); |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
121 elseif (any (strcmp ("fltk", avl_tk))) |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
122 graphics_toolkit ("fltk"); |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
123 elseif (any (strcmp ("gnuplot", avl_tk))) |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
124 graphics_toolkit ("gnuplot"); |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
125 else |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
126 error ("no valid graphics toolkit found"); |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
127 endif |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
128 endfunction |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
129 |
9088
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
130 function set_print_size () |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
131 image_size = [5.0, 3.5]; # in inches, 16:9 format |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
132 border = 0; # For postscript use 50/72 |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
133 set (0, "defaultfigurepapertype", "<custom>"); |
9912
e9fe12c1b0c0
Set default papersize to the desired imagesize
Rik <rdrider0-list@yahoo.com>
parents:
9298
diff
changeset
|
134 set (0, "defaultfigurepaperorientation", "landscape"); |
9088
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
135 set (0, "defaultfigurepapersize", image_size + 2*border); |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
136 set (0, "defaultfigurepaperposition", [border, border, image_size]); |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
137 endfunction |
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
138 |
20357
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
139 ## Use this function before plotting commands and after every call to print |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
140 ## since print() resets output to stdout (unfortunately, gnuplot can't pop |
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
141 ## output as it can the terminal type). |
9088
77e71f3da3d6
Fix documentation image printing under new development code
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
142 function hide_output () |
20357
03c88436ad8c
Correctly generate figure 15.7 of the Manual.
Rik <rik@octave.org>
parents:
20356
diff
changeset
|
143 hf = figure (1, "visible", "off"); |
6888 | 144 endfunction |
7256 | 145 |
146 ## generate something for the texinfo @image command to process | |
20350
b9d4ccf4291c
Use FLTK for creating plots for Manual.
Michael D. Godfrey <michaeldgodfrey@gmail.com>
parents:
20342
diff
changeset
|
147 function image_as_txt (d, nm) |
20351
d8992a16643c
doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
148 fid = fopen (fullfile (d, [nm ".txt"]), "wt"); |
7256 | 149 fputs (fid, "\n"); |
150 fputs (fid, "+---------------------------------+\n"); | |
151 fputs (fid, "| Image unavailable in text mode. |\n"); | |
152 fputs (fid, "+---------------------------------+\n"); | |
153 fclose (fid); | |
154 endfunction | |
20351
d8992a16643c
doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
155 |