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
gnuplot. When
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
functions like abs(x), cos(x)
or 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:
gnuplot script_to_run
When you run gnuplot in interactive mode, you can quit your session
with the command quit. The two most basic commands
are plot and
splot. plot generates
two-dimensional plots, and splot generates
three-dimensional plots. To plot a simple function, you can use:
plot sin(x)/x
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 sin(x)/x
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:
splot x**2+y**2
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:
help plot
This pulls up the help documentation that gnuplot has regarding the
plot command.
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:
splot "weather.dat"
There are two ways of customizing your plots when using gnuplot. The first
is to use options to the plot and
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
add with linespoints to your
plot command. You also can use shortcuts
for these options. For with, you can use
w. For the title option,
you can use t. For the using
option shown earlier, you can use
u.
The second option for customizing your plots is to use the
set
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:
replot
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
calling the fit command. An example would look like
this:
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
f1(x).
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
the command show. This command shows you what values have been set
within the current session. To see all of the options, use the
command 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
the 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
the command 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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- I once had a better way I
4 hours 48 min ago - Not only you I too assumed
5 hours 5 min ago - another very interesting
6 hours 58 min ago - Reply to comment | Linux Journal
8 hours 52 min ago - Reply to comment | Linux Journal
15 hours 46 min ago - Reply to comment | Linux Journal
16 hours 2 min ago - Favorite (and easily brute-forced) pw's
17 hours 53 min ago - Have you tried Boxen? It's a
23 hours 45 min ago - seo services in india
1 day 4 hours ago - For KDE install kio-mtp
1 day 4 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
translation
Hello,
I took the freedom to translate your article in french for my blog (http://libroblog.toile-libre.org/article12/gnuplot).
Of course, I made a link on your original article. I hope it's not a problem.
Best regards,
Yayel
Hello !!
Hello, I love your blog it's really nice
continued like that for the rest it is really perfect believe me.
good luck for you !!
voyance gratuite par email
gnuplot book
Thanks for this brief and useful writeup. For me, gnuplot shines brightest when I take advantage of its ability to work intimately with other systems, such as LaTeX for creating beautiful documents with seamlessly integrated graphs. If you can forgive a shameless plug, I go into this in depth in my gnuplot book.
http://detective-zakynthinos.net/
nice one...but you have to read a lot so you can work on this program.
It's quite a faf. Give me
It's quite a faf.
Give me qtiplot anyday.
Thanks for sharing
Thanks for sharing