To start exploring gnuplot, try the following commands:
plot cos(x) plot [-pi:pi] sin(x**2),cos(exp(x)) splot [-3:3] [-3:3] x**2*yThe first plot command produces a plot of
Here are explanations of the above commands. The plot command
tells gnuplot that you want to create a two-dimensional plot. In the first
example, the range of x is not specified, so gnuplot use its default
range of (-10,10). In the second example, the phrase [-pi:pi]
following plot tells gnuplot to produce the plot with x in the
range . There are two functions specified, separated by a
comma. This tells gnuplot to plot both functions on the same plot.
Note that on a color screen, gnuplot uses different colors for the
functions. On a monochrome display, the functions are plotted with
different line styles. The third command tells gnuplot to create a
three-dimensional plot (well, actually a two-dimensional projection
of a three-dimensional plot, but you know that). The two
pairs of brackets following splot set the range of the x-axis
(first set of brackets) and the range of y-axis (second set of
brackets). Note that the ranges are optional in both plot and
splot. If ranges are not specified, gnuplot uses the ranges
previously set. The ranges of y-axis in plot and the z-axis
in splot are autoscaled by default, if not specified.
If you want to specify the y-range but not the x-range, put in both sets of brackets but leave the first set empty, like
plot [] [0:2] 1/(1+x**2)The same trick works with splot.
The command set is used to control many options available in gnuplot (you have already seen set terminal). Many of the options control the appearance of the plot. In the previous examples, you saw that the ranges of the axes can be specified in the plotting command. The ranges can also be set before plotting, with the commands set xrange, set yrange, and set zrange. For example:
set xrange [-.3:3.5] set yrange [:pi**2] set zrange [exp(3.66)/sin(1.2*pi):]Note that you can omit either the upper or lower limits. The limits can be either numbers, pre-defined constants , or expressions as complicated as in the third example.
The difference between setting the ranges with plot (or splot) and with set xrange (or yrange or zrange) is that in the former case the ranges apply only to that single plot, whereas in the latter case they apply to all subsequent plots - until the ranges are reset, of course. (If the ranges are set both ways, the ones on the plot command will be used.) This is the case for all set commands.
If you have set a range and want to return to gnuplot's automatic range
selection, the command is set autoscale axis
, where
axis
is some combination of x, y, and z and, if you
omit it, all axes will be autoscaled.
You can also add axis labels and a title to the plot by the commands set xlabel, set ylabel, set zlabel, and set title. For example:
set xlabel 'x' set ylabel "Power Function" set zlabel 'Time (sec)' set title 'Some Examples' replotNote that both single quote and double quote are acceptable (but they must match). The replot command does what its name suggests; it redraws the previous plot, incorporating whatever changes have been introduced by intervening set commands. You'll be using replot a lot when you are customizing a plot (which you'll learn how to do in Section
You can also add another curve to the previous plot. Try
plot [-2*p1:2*pi] sin(x) replot tan(x)Note that the y-range adjusts itself. You cannot specify new ranges for the plot on the replot command (use the set commands), but you can do everything else that you can do with plot.