Plotting 3-dimensional data is quite straightforward, and very similar to the steps used for plotting 2-dimensional data. We will begin with an example:
> // Create the data > X = -2:2:.2; > Y = X; > Z = []; > for (i in 1:X.n) { > for (j in 1:Y.n) { > Z[i;j] = X[i] * exp (-X[i]^2 - Y[j]^2); > } > } > // Make the plot > pstart(,,"xwin"); > plwid(4); > ptitle("Sample 3-D Plot"); > xlabel("X-Axis"); > ylabel("Y-Axis"); > zlabel("Z-Axis"); > plmesh( << x = X; y = Y; z = Z >> ); > plprint("3d.ps");
The output from the plprint command appears in
Figure . Now we must take a minute and explain the data
passed as input to
plot3
. Three dimensional plots have two
independent variables (vectors), x
, and y
. The
dependent variable z
(a matrix) is a function of both
x
and y
and has row dimension the length of x
and column dimension the length y
. This storage scheme for
three-dimensional data is economical in terms of memory, and allows
the user some extra flexibility. Thus, the argument to plot3
is a list with three elements: x
, y
, and
z
. plot3
can accept up to three distinct lists for
plotting.
The same plot-functions: ptitle
, xlabel
, and
ylabel
can be used with 3-dimensional plots. Additionally,
zlabel
can be used to add labels to the z
axis.
The next example (Figure ) demonstrates more plotting
capabilities - including: changing fonts and pen widths, multiple
plots per page and annotating a plot. Note that the calls to
plptex
are made after the plot
calls. since
plptex
uses the plot coordinates to place text on the graph,
the plot must be created first.
// // Demonstrate the effect of adding terms to a Fourier expansion // // We want to approximate a square wave... // The Fourier Series for a square wave is a // sum of odd harmonics - we will demonstrate. // Start up a plot window... pstart(2,2, "xwin"); // Compute the 1st term in the Fourier series and plot. t = (0:10:.1)'; y = sin (t); ptitle ("Fundamental Frequency"); plwid(10); plfont(1); plot ( [t,y] ); plptex ("Pen width = 10", 4, 0.4); plptex ("Font = 1 (Normal)", 4, 0.1); pause (); // Now add the third harmonic to the fundamental, and plot. y = sin (t) + sin (3*t)/3; ptitle ("1st and 3rd Harmonics"); plwid(7); plfont(4); plot ( [t,y] ); plptex ("Pen width = 7", 4, 0.4); plptex ("Font = 4 (Script)", 4, 0.1); pause (); // Now use the first, third, fifth, seventh, and ninth harmonics. y = sin (t) + sin (3*t)/3 + sin (5*t)/5 + sin (7*t)/7 + sin (9*t)/9; ptitle ("1st, 3rd, 5th, 7th, 9th Harmonics"); plwid(4); plfont(3); plot ( [t,y] ); plptex ("Pen width = 3", 4, 0.4); plptex ("Font = 3 (Italic)", 4, 0.1); pause (); // // Now create a matrix with rows that represent adding // more and more terms to the series. // t = (0:3.14:.02); y = zeros(10,max(size(t))); x = zeros(size(t)); for (k in 1:19:2) { x = x + sin(k*t)/k; y[(k+1)/2;] = x; } // // Now make a nice 3-D plot that shows the effect // of adding more and more terms to the series. // plfont (2); plwid(1); ptitle ("Square Wave via Fourier Series"); ylabel ("No. Terms"); plaz (140); plot3 (<< x = t; y=1:10; z=y' >>); plptex ("Pen width = 1", -1, 4.5); plptex ("Font = 2 (Roman)", -1, 3.8); plprint ("p6.ps"); // Make hardcopy (Postscript default).