3 * (1) wait for user between examples:
4 * demo ("tests/rtest_plot.mac");
6 * (2) or noninteractively:
7 * batch ("tests/rtest_plot.mac");
9 * Maxima waits for the user to close the Openmath window
10 * before proceeding, but otherwise it just blazes through.
13 /* Examples copied from plot2d documentation */
15 "Regression for f0 issue"$
16 plot2d(x^2,[x,-1,1],[yx_ratio,1]);
17 plot2d(x^2,[x,-1,1],[xy_scale,1,1]);
18 plot3d(x^2,[x,-1,1],[y,-1,1],[zmin,1]);
19 plot3d(x^2,[x,-1,1],[y,-1,1],[xtics,1]);
20 plot3d(x^2,[x,-1,1],[y,-1,1],[ytics,1]);
21 plot3d(x^2,[x,-1,1],[y,-1,1],[ztics,1]);
22 plot3d(x^2,[x,-1,1],[y,-1,1],[xtics,1,1]);
23 plot3d(x^2,[x,-1,1],[y,-1,1],[ytics,1,1]);
24 plot3d(x^2,[x,-1,1],[y,-1,1],[ztics,1,1]);
25 plot3d(x^2,[x,-1,1],[y,-1,1],[color_bar_tics,1]);
26 plot3d(x^2,[x,-1,1],[y,-1,1],[label,"tako",1,1]);
27 plot3d(x^2,[x,-1,1],[y,-1,1],[label,["tako",1,1],["surume",1,1],["ika",1,1]]);
29 "Plots of common functions." $
31 plot2d (sin(x), [x, -5, 5])$
32 plot2d (sec(x), [x, -2, 2], [y, -20, 20], [nticks, 200])$
34 "Plotting functions by name." $
37 :lisp (defun |$g| (x) (m* x x x))
38 H(x) := if x < 0 then x^4 - 1 else 1 - x^5 $
39 plot2d (F, [u, -1, 1])$
40 plot2d ([F, G, H], [u, -1, 1], [y, -1.5, 1.5])$
42 "We can plot a circle using a parametric plot ..." $
44 plot2d ([parametric, cos(t), sin(t), [t,-%pi,%pi], [nticks,80]], [x, -4/3, 4/3])$
46 "If we repeat that plot with only 8 points ..." $
47 "As parametric plot uses adaptive method, nticks=4 and adapt_depth=1 is for 8 point drawing" $
49 plot2d ([parametric, cos(t), sin(t), [t, -%pi*2, %pi*2], [nticks, 4], [adapt_depth,1]], [x, -2, 2], [y, -1.5, 1.5])$
51 "Combination of an ordinary plot ..." $
53 plot2d ([x^3+2, [parametric, cos(t), sin(t), [t, -5, 5], [nticks, 80]]], [x, -3, 3])$
55 "Example of a logarithmic plot:" $
57 plot2d (exp(3*s), [s, -2, 2], [logy])$
59 "To show some examples of discrete plots, ..." $
61 xx:[10, 20, 30, 40, 50]$
62 yy:[.6, .9, 1.1, 1.3, 1.4]$
63 xy:[[10,.6], [20,.9], [30,1.1], [40,1.3], [50,1.4]]$
64 plot2d([discrete,xx,yy])$
66 "We will now show the plot with only points, ..." $
68 plot2d([discrete, xy], [style, points])$
70 "The plot of the data points can be shown together with ..." $
72 plot2d([[discrete,xy], 2*%pi*sqrt(l/980)], [l,0,50],
73 [style, [points,5,2,6], [lines,1,1]], [legend,"experiment","theory"],
74 [xlabel,"pendulum's length (cm)"], [ylabel,"period (s)"])$
76 "To save a plot ..." $
78 plot2d (sin(x), [x, 0, 2*%pi], [ps_file, concat (maxima_tempdir, "/sin.eps")])$
80 "The next example uses the y option ..." $
82 plot2d ([gamma(x), 1/gamma(x)], [x, -4.5, 5], [y, -10, 10], [gnuplot_preamble, "set key bottom"])$
84 "We can also use a `gnuplot_preamble' to produce fancy x-axis labels." $
86 my_preamble: "set xtics ('-2pi' -6.283, \
87 '-3pi/2' -4.712, '-pi' -3.1415, '-pi/2' -1.5708, '0' 0, \
88 'pi/2' 1.5708, 'pi' 3.1415,'3pi/2' 4.712, '2pi' 6.283)"$
89 plot2d([cos(x), sin(x), tan(x), cot(x)], [x, -2*%pi, 2.1*%pi], [y, -2, 2],
90 [axes, x], [gnuplot_preamble, my_preamble]);
92 "... fancy x-axis labels, and produces PostScript output ..." $
94 my_preamble: "set xtics ('-2{/Symbol p}' \
95 -6.283, '-3{/Symbol p}/2' -4.712, '-{/Symbol p}' -3.1415, \
96 '-{/Symbol p}/2' -1.5708, '0' 0,'{/Symbol p}/2' 1.5708, \
97 '{/Symbol p}' 3.1415,'3{/Symbol p}/2' 4.712, '2{/Symbol p}' \
99 plot2d ([cos(x), sin(x), tan(x)], [x, -2*%pi, 2*%pi],
100 [y, -2, 2], [gnuplot_preamble, my_preamble], [ps_file, concat (maxima_tempdir, "/trig.eps")]);
102 /* Examples copied from plot3d documentation */
104 "Displays a plot of one or three expressions as functions of two variables." $
106 plot3d (2^(-u^2 + v^2), [u, -3, 3], [v, -2, 2]);
108 "The same graph can be plotted using openmath ..." $
110 plot3d (2^(-u^2 + v^2), [u, -3, 3], [v, -2, 2], [plot_format, openmath]);
112 "An example of the third pattern of arguments is" $
114 plot3d ([cos(x)*(3 + y*cos(x/2)), sin(x)*(3 + y*cos(x/2)),
115 y*sin(x/2)], [x, -%pi, %pi], [y, -1, 1], ['grid, 50, 15]);
117 "This example shows a plot of the real part of `z^1/3' ..." $
119 plot3d (r^.33*cos(th/3), [r, 0, 1], [th, 0, 6*%pi], ['grid, 12, 80],
120 ['transform_xy, polar_to_xy], [box, false],[legend,false]);
122 "Other examples are the Klein bottle:" $
124 expr_1: 5*cos(x)*(cos(x/2)*cos(y) + sin(x/2)*sin(2*y)+ 3.0) - 10.0$
125 expr_2: -5*sin(x)*(cos(x/2)*cos(y) + sin(x/2)*sin(2*y)+ 3.0)$
126 expr_3: 5*(-sin(x/2)*cos(y) + cos(x/2)*sin(2*y))$
127 plot3d ([expr_1, expr_2, expr_3], [x, -%pi, %pi], [y, -%pi, %pi], ['grid, 40, 40]);
131 expr_1: cos(y)*(10.0+6*cos(x))$
132 expr_2: sin(y)*(10.0+6*cos(x))$
134 plot3d ([expr_1, expr_2, expr_3], [x, 0, 2*%pi], [y, 0, 2*%pi], ['grid, 40, 40]);
136 "Sometimes it is necessary to define a function to plot the expression." $
138 M: matrix([1, 2, 3, 4], [1, 2, 3, 2], [1, 2, 3, 4], [1, 2, 3, 3])$
139 f(x, y) := float (M [?round(x), ?round(y)])$
140 plot3d (f, [x, 1, 4], [y, 1, 4], ['grid, 4, 4])$
142 "Here is a three-dimensional plot using the gnuplot pm3d terminal." $
144 plot3d (atan (-x^2 + y^3/4), [x, -4, 4], [y, -4, 4], [grid, 50, 50], [gnuplot_pm3d, true])$
146 "And a three-dimensional plot without a mesh and with contours ..." $
148 my_preamble: "set pm3d at s;unset surface;set contour;\
149 set cntrparam levels 20;unset key"$
150 plot3d(atan(-x^2 + y^3/4), [x, -4, 4], [y, -4, 4], [grid, 50, 50],
151 [gnuplot_pm3d, true], [gnuplot_preamble, my_preamble])$
153 "Finally, a plot where the z-axis is represented by color only." $
155 plot3d (cos (-x^2 + y^3/4), [x, -4, 4], [y, -4, 4],
156 [gnuplot_preamble, "set view map; unset surface"], [gnuplot_pm3d, true], [grid, 150, 150])$
158 /* Examples copied from plot_options documentation */
160 "Option: `plot_realpart'" $
162 plot2d (log(x), [x, -5, 5], [plot_realpart, false]);
163 plot2d (log(x), [x, -5, 5], [plot_realpart, true]);
165 /* further *plot-realpart* examples from mailing list 2010-08-13 "plot3d: function not defined everywhere in the plotting range" */
167 plot2d (log(x), [x, -3, 3]);
168 plot3d (log(x), [x, -3, 3], [y, -3, 3]);
169 contour_plot (log(x), [x, -3, 3], [y, -3, 3]);
171 /* (other options listed under plot_options do not have plot2d or plot3d examples) */
173 /* Examples copied from contour_plot documentation */
175 "contour_plot examples" $
177 contour_plot (x^2 + y^2, [x, -4, 4], [y, -4, 4]);
179 contour_plot (sin(y) * cos(x)^2, [x, -4, 4], [y, -4, 4]);
181 "contour_plot with named function" $
183 F(x, y) := x^3 + y^2;
184 contour_plot (F, [u, -4, 4], [v, -4, 4]);
186 "contour_plot with gnuplot_preamble" $
188 contour_plot (F, [u, -4, 4], [v, -4, 4], [gnuplot_preamble, "set size ratio -1"]);
190 "contour_plot with gnuplot_preamble via set_plot_option" $
192 set_plot_option ([gnuplot_preamble, "set cntrparam levels 12"])$
193 contour_plot (F, [u, -4, 4], [v, -4, 4]);
195 /* Examples adapted from demo/demo.dem
196 * (others are duplicates or not different enough)
199 "two dimensional parametric plot" $
201 plot2d ([parametric, t*sin(t), t*cos(t)], [t, 0, 80], [nticks, 1000]);
203 "three dimensional cartesian plot of a bessel function" $
205 plot3d (bessel_j (0, sqrt(x^2 + y^2)), [x, -12, 12], [y, -12, 12]);
207 "three dimensional polar plot of the same bessel function" $
209 plot3d (bessel_j (0, r), [th, 0, 2*%pi], [r, 0, 12], ['transform_xy, polar_to_xy]);
211 "three dimensional plot of x*exp(-x^2-y^2)" $
213 plot3d (x*exp(- x^2 - y^2), [x, -2, 2], [y, -2, 2]);
215 /* Example adapted from demo/plots.mac
216 * (others are duplicates or not different enough)
219 "Real part of z^1/6" $
221 plot3d (r^(1/6.0)*cos(th/6), [r, 0, 1], [th, 0, 2*6*%pi], ['grid, 12, 121], ['transform_xy, polar_to_xy]);
223 /* Examples related to bug reports or other specific topics */
225 /* SF bug [ 1699445 ] plot2d in very narrow range
226 * triggers Gnuplot bug on Windows (line outside bounding box)
229 "[ 1699445 ] plot2d in very narrow range" $
231 plot2d (sin(x), [x, 1.57079628, 1.570796326794897]);
233 "[ 2234113 ] plot2d odd roots of X plots only positive values" $
235 plot2d (x^(1/3), [x, -5, 5]);
237 plot2d (u^(1/5), [u, -5, 5]);
239 "r1.55 src/plot.lisp: ensure that log plots are adequately sampled" $
241 plot2d (x, [x, 1e-5, 100], [logy]);
243 plot2d (x, [x, 1e-5, 100], [logx]);
245 "r1.62 src/plot.lisp: expand the cases recognized by COERCE-FLOAT-FUN" $
247 :lisp (defun $f (x) (+ (cl:sin x) 0.1))
248 :lisp (defmspec $h (x) (+ (cl:sin (cadr x)) 0.3))
249 (g(x) := sin(x) + 0.2,
250 i(x) ::= sin(x) + 0.4,
252 "j"(x) := sin(x) + 0.5,
253 matchdeclare (x, floatnump),
254 tellsimpafter (k(x), sin(x) + 0.6));
256 plot2d ([f, g, h, i, sin, k], [u, 0, 1]);
258 /* "j" in list of functions tickles a bug -- quote marks not sanitized
259 * work around it by explicit legend spec
261 plot2d (["+", "j"], [u, 0, 1], [legend, "+", "j"]);
263 "(not a plot example, maybe it should be moved elsewhere)" $
265 map (lambda ([f], quad_qags (f, u, 0, 1)), [f, g, h, i, "+", "j", sin, k]);
267 "r1.82 src/plot.lisp: floatify non-float numbers" $
269 plot2d ([discrete, [1, 2, 3, 4], [1, 1/2, 1/3, 1/4]]);
271 plot2d ([discrete, [1, 2, 3, 4], [1/%e, 1/%pi, 1/%phi, 1/%gamma]]);
273 plot2d ([discrete, [1, 2, 3, 4], [1b0, 0.5b0, 0.33b0, 0.25b0]]);
275 "SF bug [ 2672976 ] wxMaxima 0.8.1: set logscale x gives error";
277 plot2d (sin(x), [x, 0, 1], [logx, true]);
279 plot2d (sin(u), [u, 0, 1], [logx, true]);
281 plot2d (sin(u), [u, 0, 1], [logx, true], [y, 0, 1], [logy, true]);
283 "verify that nested numerical integral is handled correctly";
285 plot2d (w^2 * 'quad_qags (1/(s - w), s, 1, 5) [1], [w, -5, -1], [adapt_depth, 1]);
287 "another nested numerical integral";
290 W(t) := 95*sqrt(t)*sin(t/6)^2,
291 R(t) := 275*sin(t/3)^2,
292 A(t) := 1200 + quad_qags (W(x) - R(x), x, 0, t) [1],
293 plot2d (A(t), [t, 0, 18], [adapt_depth, 1]));
295 "plotting realpart and imagpart";
297 plot3d (realpart (asin (x + y*%i)), [x, -2, 2], [y, -2, 2], [grid, 20, 20]);
299 plot3d (imagpart (asin (x + y*%i)), [x, -2, 2], [y, -2, 2], [grid, 20, 20]);
301 plot3d (realpart (exp (x + y*%i)), [x, -2, 2], [y, -2, 2], [grid, 20, 20]);
303 plot3d (imagpart (exp (x + y*%i)), [x, -2, 2], [y, -2, 2], [grid, 20, 20]);
305 "messages about non-numeric values and clipped values";
307 plot2d (sqrt (x), [x, 0, 1]); /* no message */
309 plot2d (sqrt (x), [x, -1, 1]); /* some non-numeric */
311 plot2d (sqrt (x), [x, 0, 1], [y, 0, 1/2]); /* some clipped */
313 plot2d (sqrt (x), [x, -1, 1], [y, 0, 1/2]); /* some non-numeric, some clipped */
315 plot2d (sqrt (x), [x, -2, -1]); /* all non-numeric */
317 plot2d (sqrt (x), [x, 0, 1], [y, -2, -1]); /* all clipped */
319 plot2d (sqrt (x), [x, -1, 1], [y, -2, -1]); /* all non-numeric or clipped */
321 "coping with overflow in intermediate results";
323 /* from the mailing list "plot numerical question" 2009-03-11 */
325 (r4(s) := block ([s : bfloat(s)], s!/(((s/4)!)^4 * 4^s)),
326 plot2d (r4, [s, 200, 300], [adapt_depth, 1], [nticks, 5]));
328 plot2d ('r4(s), [s, 200, 300], [adapt_depth, 1], [nticks, 5]);
330 /* from mailing list 2009-02-18
331 * "Re: [Maxima] I want to tell maxima (-1)^0.33333333333333=-1, what should i do?"
334 foo29(x):=(sqrt(-16*x^4-16*x^3+20*x^2+12*x+23)/(6*sqrt(3))+(16*x^3-12*x^2-6*x-25)/54)^(1/3)$
336 plot2d (foo29 (u), [u, -1, 0]);
338 plot2d (foo29, [u, -1, 0]);
342 plot2d (foo29, [u, -1, 0]);
344 /* bug report "Wrong result given by coerce-float-fun" ID: 2880115 */
347 f(k) := integrate (exp(%i*k*x)*sin(x)/x, x, minf, inf),
348 plot2d (f, [x, -3, 3], [adapt_depth, 1], [nticks, 5]));
351 plot2d (f, [x, -3, 3], [adapt_depth, 1], [nticks, 5]));
353 /* bug report "xlabel and ylabel don't change plot3d axis labels" - ID: 3020589 */
355 (Bxt(x, r) := x^2 + r^2,
357 plot3d(Bxt(x,r),[x,0,L],[r,0,R],[xlabel,"x [m]"],[ylabel,"r [m]"],[zlabel,"Bx [T]"],[legend,"Axial field"]));
359 /* same but now using default axis labels */
360 plot3d(Bxt(x,r),[x,0,L],[r,0,R],[legend,"Axial field"]);
362 /* from the mailing list 2011-05-26:
363 * "Wrong usage" error message when trying to plot with plot3d or contour_plot
364 * should expect this example to provoke an advisory message and make a plot
367 block (local (fn, Fn, pw, fbn, fbnxcy, loww),
368 fn(x):=1/sqrt(2*%pi)*exp((-x^2)/2),
369 Fn(x):=integrate(fn(t),t,-inf,x),
370 pw(r1,r2,s1,s2):=1-Fn((r2-r1)/sqrt(s1^2+s2^2)),
371 fbn(x,y,r):=1/(2*%pi*sqrt(1-r^2))*exp((-(x^2-2*r*x*y+y^2))/(2*(1-r^2))),
372 fbnxcy(x,y,r) := fbn(x,y,r) / fn(y),
373 loww(rs2, ro, r, s1, s2) := quad_qagi(fbnxcy(rs1, rs2, r)*pw(rs1, ro, s1, s2), rs1, minf, inf),
374 plot3d( 'loww(rs2, 0, r, 1, 1)[1] , [rs2, -1.5, 2.5], [r, 0.1, 0.9] ));
376 /* simpler version of the preceding one */
379 foo(x, y) := if numberp(x) and numberp(y) then [x^2 - y^2] else funmake (foo, [x, y]),
380 plot3d (foo (a, b)[1], [a, -1, 1], [b, -1, 1]));
382 /* helix -- see mailing list 2012-01-22 "3D curve parametric plot" */
384 plot3d([sin(t), cos(t), t], [t,-5,5], [y,-5,5], [grid,100,2], [gnuplot_pm3d,false])$
386 /* plot2d/plot3d with subscripted variable */
388 (kill (x, a), plot2d (a[x]^3, [a[x], -1, 1]));
390 plot3d (a[x]^2 - x[a]^3, [a[x], -1, 1], [x[a], -1, 1]);
392 /* from wxMaxima forum 2013-02-16: "plot2d: expression evaluates to non-numeric value everywhere in plotting range"
393 * https://sourceforge.net/p/wxmaxima/discussion/435775/thread/d89ea980/
396 F(omega) := abs((%i * omega + 2E4)^2);
397 plot2d(F(omega), [omega, 0.01, 2E5]);