Gnuplot—the Grandfather of Graphing Utilities
In these columns, I have covered several different scientific packages for doing calculations in many different areas of research. I also have looked at various packages that handle graphical representation of these calculations. But, one package that I've never looked at before is gnuplot (http://www.gnuplot.info). Gnuplot has been around since the mid-1980s, making it one of the oldest graphical plotting programs around. Because it has been around so long, it's been ported to most of the operating systems that you might conceivably use. This month, I take a look at the basics of gnuplot and show different ways to use it.
Gnuplot is a command-line-driven program. As such, it has been co-opted to provide graphic capabilities in several other applications, such as octave. Thus, you may have used gnuplot without even realizing you were doing so. You can use gnuplot in several ways. It not only can accept input data to plot, but it also can plot functions. Gnuplot can send its output either to the screen (in both a static file format display or an interactive display), or it can send output to any of a large number of file formats. Additionally, lots of functions are available to customize your plots, changing the labels and axes, among other things.
Let's start by installing gnuplot. Binaries are available for many different operating systems. Most Linux distributions also should come with a package for gnuplot, so installation should be a breeze. If you want the latest and greatest features available, you always can download the source code and build gnuplot from scratch.
Once gnuplot is
installed, you can start it by executing the command
executed this way, you are launched into an interactive session. Let's
start by trying to plot a basic function. You should be able to plot
any mathematical function that would be accepted in C, FORTRAN or
BASIC. These mathematical expressions can be built up from built-in
Bessel. You can use integer,
real and complex data types as arguments to these functions.
When using gnuplot to generate a plot, you either can have all of the commands in a single file and hand them in to gnuplot as a script, or you can start gnuplot up in interactive mode and issue these commands one at a time in the command environment. To run a gnuplot script, you simply need to add it at the end of the command when you run gnuplot—for example:
When you run gnuplot in interactive mode, you can quit your session
with the command
quit. The two most basic commands
two-dimensional plots, and
three-dimensional plots. To plot a simple function, you can use:
This generates a plot window, displaying the graphical results (Figure 1). If you want to add a title to the plot, you can add this option to the plot command:
plot sin(x)/x title "Example 1"
Figure 1. Plotting commands open a new window for display.
Figure 2. A Basic Plot of
You even can plot multiple expressions on the same plot window with:
plot sin(x)/x title "Example 1", sin(x) title "Example 2"
Figure 3. You can plot multiple functions on the same graph.
To plot a three-dimensional graph, simply hand in an expression with two
independent variables to
splot, such as:
Figure 4. Gnuplot even can handle 3-D plots.
If you run into a problem, the first place to look is the built-in help
function. To get help with the
plot command, execute the command:
This pulls up the help documentation that gnuplot has regarding the
This is fine if you are just trying to see what some expression looks like when it is plotted out, but in real science, you often collect data in experiments that need to be plotted so you can do some graphical analysis and get ideas as to what may be happening. Gnuplot can handle this type of plotting too. To do so, you simply need to hand in the filename of the file containing the data to be plotted. This file should have the data elements arranged in columns, where the columns are separated by white space of some kind. Any lines that start with # are treated as comments by gnuplot and are ignored. If your data file contains several data columns, you can select which columns are pulled in to be plotted as options to the plot or splot functions. As an example, say you have a data file that has the temperature and pressure for each day. You can plot the temperature with:
plot "weather.dat" using 1:2 title "Temperature"
If you want to get the pressure graph, you would use:
plot "weather.dat" using 1:3 title "Pressure"
If you want to plot all three columns, you can use:
There are two ways of customizing your plots when using gnuplot. The first
is to use options to the
splot commands. In this case, you define
things like the title of the plot, the axes or the style. The styles
available can be lines, points, linespoints, impulses, dots, steps,
fsteps, histeps, errorbars, xerrorbars, yerrorbars or xyerrorbars. To use
one of the styles, you can include the option with the
with keyword. So,
if you want to plot both the lines and points of your graph, you could
with linespoints to your
plot command. You also can use shortcuts
for these options. For
with, you can use
w. For the
you can use
t. For the
option shown earlier, you can use
The second option for customizing your plots is to use the
command. With this command, you are free to set the values for several
graphing options. Using the second option, you can set all types of
options, like the title, xlabel, yrange, xtics or key, among other
options. For example, you can set the y-range with:
set yrange [20:500]
After setting the various plotting options, you need to tell gnuplot to redraw the plot you are working on. You can do this with the command:
Many of these
set options also use shortcuts. For example, the shortcut
version of the above command is:
set yr [20:500]
Gnuplot is not only a capable utility to plot data and functions,
but it also can do some analysis on the data being plotted. For example,
you can get gnuplot to do curve fitting on the data. To do so, you
first need to define a function, as well as some initial guesses before
fit command. An example would look like
f1=a1*tanh(x/b1) a1=300; b1=0.005; fit f1(x) 'data_file.dat' using 1:2 via a1,b1
This tells gnuplot to try to fit the data from the columns 1 and
2 from the file data_file.dat to the function defined by
When you have an environment created for a particular research area, you
can save all of the settings you may have set up with the command
save. This command essentially saves off all of the gnuplot
commands you issued to the text file. This text file can be
loaded into a new gnuplot session with the
load command. This will
take all of the commands saved to the save file and re-run them in
the new session.
You always can see what options have been set by using
show. This command shows you what values have been set
within the current session. To see all of the options, use the
show all. When you are playing with options, you sometimes
can get yourself into an odd condition. Just remember that you always
can reset any values created with the
set by using
reset command. This
command resets these session options to their default values.
Sometimes you may need to interact with the system on which gnuplot is
running. In those cases, you need to start a shell session from
gnuplot. There are two ways to do so. The first is to use the command
system. In this case, you can hand in a string containing the system
commands that need to be run outside of gnuplot. The other option is
to use the command
!. This command actually is just a shortcut for
system, and the commands can be used interchangeably.
This article has covered only the most basic functions available in gnuplot. It's definitely worth your time to look deeper into the documentation to see what else it can do for you in analyzing your data. Even if you don't use gnuplot directly, learning more about it will help you when you use other applications like octave. Take this article as a jumping-off point and explore just what is possible in data analysis.
Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.
Free DevOps eBooks, Videos, and more!
Regardless of where you are in your DevOps process, Linux Journal can help!
We offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, and advice & help from the expert sources like:
- Linux Journal
- Users, Permissions and Multitenant Sites
- New Products
- Flexible Access Control with Squid Proxy
- Security in Three Ds: Detect, Decide and Deny
- High-Availability Storage with HA-LVM
- Tighten Up SSH
- DevOps: Everything You Need to Know
- Solving ODEs on Linux
- Non-Linux FOSS: MenuMeters
- March 2015 Issue of Linux Journal: System Administration