0% found this document useful (0 votes)
13 views3 pages

Gnuplot Basics for Data Plotting

This document provides examples of basic commands for generating plots using the Gnuplot computational physics software. It describes how to plot data from files, choose axis ranges, plot multiple datasets on the same graph, plot functions along with data, take logarithms of data, write plots to files, and execute Gnuplot commands from an input file.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Gnuplot Basics for Data Plotting

This document provides examples of basic commands for generating plots using the Gnuplot computational physics software. It describes how to plot data from files, choose axis ranges, plot multiple datasets on the same graph, plot functions along with data, take logarithms of data, write plots to files, and execute Gnuplot commands from an input file.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] is a text le containing two columns of (space separated) data. To make
a plot of those data points,
plot [Link]
To instead draw a continuous line joining the points,
plot [Link] with lines
To plot data from two les on the same plot, any of the following:
plot [Link], [Link]
plot [Link] with lines, [Link]
plot [Link] with lines, [Link] with lines
To choose yourself the range of the horizontal axis, e.g. 1020,,
plot [10:20] [Link]
To also choose the range of the vertical axis, e.g. 6070,
plot [10:20] [60:70] [Link]
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), [Link]
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 [Link] 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 [Link] using 1:2, [Link] using 1:3
Suppose you want to plot the log of column 2 vs. the value of column 1:
plot [Link] using 1:(log($2))
Or the log vs. the log:
plot [Link] 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 [Link]
When you are done with Gnuplot, your plot will be stored in the postscript le [Link]
(or whatever you named it above). You can look at postscript by invoking the gv program
from your unix shell, e.g.
2
gv [Link]
(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 [Link]
(which is again a command for the Unix shell, not a gnuplot command). This will create a
le [Link] 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 [Link] with your gnuplot commands. Then you can invoke gnuplot from your
Unix shell with the name of the le with your commands:
gnuplot [Link]
3

You might also like