Sie sind auf Seite 1von 3

Physics 5640: Computational Physics II

Gnuplot Examples
Invoking and leaving Gnuplot
Just run gnuplot from your Unix shell with the command
gnuplot
To get out of gnuplot, enter the gnuplot command
exit
(or hit ctrl-D, which means end of input).
Examples of basic plot commands
Suppose le2.dat is a text le containing two columns of (space separated) data. To make
a plot of those data points,
plot file.dat
To instead draw a continuous line joining the points,
plot file.dat with lines
To plot data from two les on the same plot, any of the following:
plot file1.dat, file2.dat
plot file1.dat with lines, file2.dat
plot file1.dat with lines, file2.dat with lines
To choose yourself the range of the horizontal axis, e.g. 1020,,
plot [10:20] file.dat
To also choose the range of the vertical axis, e.g. 6070,
plot [10:20] [60:70] file.dat
If you want to set the ranges permanently, you could instead do the separate commands
set xrange [10:20]
set yrange [60:70]
before doing your plots. If you want a square plot area rather than a rectangular one, do
set size square
1
before issuing your plot commands. Or, if youve already issued them, enter the above
command and then type
replot
to replot the last plot.
To plot a simple function along with your data, e.g. 3 + 2 sin(x),
plot 3+2*sin(x), file.dat
By the way, exponentiation is ** in Gnuplot. [See help expressions and its subtopics,
e.g. help operators binary.] If you have more than two columns of data in your le, you
can plot, for example, column 5 (vertical axis) vs. column 2 (horizontal axis) with
plot file.dat using 2:5
Or suppose the rst column is your x and column 2 is one function you want to plot, and
column 3 is another. You can plot both functions at the same time with
plot file.dat using 1:2, file.dat using 1:3
Suppose you want to plot the log of column 2 vs. the value of column 1:
plot file.dat using 1:(log($2))
Or the log vs. the log:
plot file.dat using (log($1)):(log($2))
[In a using description, a number by itself refers to a column. In contrast, and expression
in parenthesis there means that it should evaluate that expression, in which case $1, $2, etc.
refer to the values of whats in columns 1, 2, etc.]
But if its really a log-linear plot youre after, there is a much simpler and more elegant
way to get it: Type the letter l into the window that contains your plot. This acts as a
toggle between linear-linear and log-linear plotting. (Alternatively, in the window running
gnuplot, you can use the commands set logscale y or unset logscale y, followed by a
replot, to toggle back and forth.)
Writing plots to a le
If you want your plot to go to a le so you can later print it, or include it in another
document, or whatever), the before you do your plot commands, give the Gnuplot commands
set terminal postscript
set output outfile.ps
When you are done with Gnuplot, your plot will be stored in the postscript le output.ps
(or whatever you named it above). You can look at postscript by invoking the gv program
from your unix shell, e.g.
2
gv output.ps
(Thats a Unix shell command, not a postscript command, and assumes that you have the
GhostView program installed on whatever computer you are using.) Alternatively, you can
convert a postscript le to PDF with
ps2pdf output.ps
(which is again a command for the Unix shell, not a gnuplot command). This will create a
le output.pdf which you can look at with Adobe reader or whatever.
Gnuplot input les
The easiest way to re-execute previous gnuplot commands is to call them up with the
up-arrow key and then hit return. Sometimes, though, you would like to put a set of gnuplot
commands in a le so that you can execute them whenever you want. Suppose you have a
le commands.plt with your gnuplot commands. Then you can invoke gnuplot from your
Unix shell with the name of the le with your commands:
gnuplot commands.plt
3

Das könnte Ihnen auch gefallen