Book Image

gnuplot Cookbook

By : Lee Phillips
Book Image

gnuplot Cookbook

By: Lee Phillips

Overview of this book

gnuplot is the world's finest technical plotting software, used by scientists, engineers, and others for many years. It is in constant development and runs on practically every operating system, and can produce output in almost any format. The quality of its 3d plots is unmatched and its ability to be incorporated into computer programs and document preparation systems is excellent. gnuplot Cookbook ñ it will help you master gnuplot. Start using gnuplot immediately to solve your problems in data analysis and presentation. Quickly find a visual example of the graph you want to make and see a complete, working script for producing it. Learn how to use the new features in gnuplot 4.4. Find clearly explained, working examples of using gnuplot with LaTeX and with your own computer programming language. You will master all the ins and outs of gnuplot through gnuplot Cookbook. You will learn to plot basic 2d to complex 3d plots, annotate from simple labels to equations, integrate from simple scripts to full documents and computer progams. You will be taught to annotate graphs with equations and symbols that match the style of the rest of your text, thus creating a seamless, professional document. You will be guided to create a web page with an interactive graph, and add graphical output to your simulation or numerical analysis program. Start using all of gnuplot's simple to complex features to suit your needs, without studying its 200 page manual through this Cookbook.
Table of Contents (18 chapters)
gnuplot Cookbook
About the Author
About the Reviewers
Finding Help and Information


Why gnuplot?

gnuplot is a free, open source plotting program that has been in wide use since 1986. It's used as the graphics backend by many other programs, so plenty of people use gnuplot without knowing it. If you've used Octave, Maxima, statist, gretl, or the Emacs graphing calculator, you've already used gnuplot.

gnuplot was originally designed to visualize scientific data, but its use has expanded to encompass every domain where sophisticated and accurate plotting is required. gnuplot is used in science, engineering, sociology, mapping, business, finance, and computer systems and network monitoring.

gnuplot excels at complex 3D graphing with hidden-line removal and at the rendering of surfaces and contours. It can produce almost any type of graph imaginable (except for pie-charts—but it can be convinced to do this, too, as we'll show later!) for a dizzying array of output devices, and can save plots in almost any type of common file format (and some uncommon ones). It can be installed on any type of computer system you are likely to encounter; there are binaries available for Windows and the sources can be compiled on most reasonably modern machines. I have compiled the latest version (4.4) of gnuplot on both Linux and Macintosh (OS X) computers and verified that all of its advanced features are fully available on both of these architectures. The recipes in this book that illustrate features newly appearing in version 4.4 are marked with [new].

gnuplot can easily be automated. It has its own scripting language and can be controlled from many general-purpose programming languages. gnuplot can also be incorporated into various publishing and document creation workflows to help create professional books, papers, and online documents.

Why this book?

Because of gnuplot's many years of deployment and sophisticated community of expert users, help is usually easy to find in some form. If you are trying to solve a tricky plotting problem, there is a reasonable chance that someone online has either figured it out or is willing to share some ideas about how it might be done.

However, there is little available in the form of a convenient reference with the structure of a cookbook, where you can look for an example of the type of plot you are trying to create and see instantly how it can be done, with a runnable example.

This book is designed to be that combination of reference and tutorial. It goes beyond plotting recipes, however, and will show you how to incorporate your graphs into documents, how to create interactivity, how to program and automate gnuplot, and more. Each example is in the form of a recipe with immediately runnable code in electronic form, and with clear explanations that will show you how to modify the recipe to solve your particular problem. Each recipe is illustrated with the plot created by the procedure, so you can use the book as a visual index that will allow you to quickly find the solution you are looking for.

One of our goals is to show you the major new features in the latest release version of gnuplot, version 4.4.3. Even experienced users of gnuplot are likely to find these sections useful, as we include an illustrative recipe for each new feature; these are specially marked so that features making their first appearance in gnuplot 4.4 can be located quickly. These new features include the use of Unicode characters, transparency, new graph positioning commands, plotting objects, internationalization, circle plots, interactive HTML5 canvas plotting, iteration in scripts, lua/tikz/LaTeX integration, cairo and SVG terminal drivers, and volatile data.

What this book covers

Chapter 1, Plotting Curves, Boxes, Points, and more, covers the basic usage of Gnuplot: how to make all kinds of 2D plots for statistics, modeling, finance, science, and more.

Chapter 2, Annotating with Labels and Legends, explains how to add labels, arrows, and mathematical text to our plots.

Chapter 3, Applying Colors and Styles, covers the basics of colors and styles in gnuplot, plus transparency, and plotting with points and objects.

Chapter 4, Controlling Your Tics, will show you how to get your tic marks and labels just right, along with gnuplot's new internationalization features.

Chapter 5, Combining Multiple Plots, shows how to arrange a set of graphs on the page, and make inset plots.

Chapter 6, Including Plots in Documents, delves into incorporating your plots into technical documents, presentations, and web pages.

Chapter 7, Programming gnuplot and dealing with data, covers how to use gnuplot's built-in programming constructs as well as its ability to be used from any programming language, and how to use the new volatile data features.

Chapter 8, The Third Dimension, shows how to plot surfaces, vectors, heat maps, and lines in a 3D space.

Chapter 9, Using and Making Graphical User Interfaces, introduces several GUIs for gnuplot and includes writing a web application with gnuplot on the backend.

Chapter 10, Surveying Special Topics, covers several special techniques and applications: mapping; labeled contours; colored and broken axes; pictures; and more.

Appendix, Finding help and information, provides a brief list of sources of gnuplot information and education.

What you need for this book

The prerequisites for this book are that you have an installation of gnuplot available for your use and that you are familiar with elementary gnuplot operation (starting gnuplot on the command line and entering a plot command). You should be able to create plots on one of the screen terminals and to save a plot file, as well. Other than that, no specialized knowledge is required to make use of this guide; although we may take examples from various specialized fields, they are incidental to the recipes, which are focused on creating particular types of graphs. The examples of controlling gnuplot from programming languages use simple examples that can be understood even if you don't have experience with the languages used in the recipes.

Who this book is for

Whether you are an old hand at gnuplot or have just started using it, this book is a convenient visual reference that covers the full range of gnuplot's capabilities, including its latest features. This volume is ideal for the gnuplot user who needs complete, runnable scripts to solve specialized graphing problems and clear explanations that will allow him or her to immediately modify them for the tasks at hand.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "To make a file with data that forms a parabola flipped upside down, tell gnuplot to set table 'parabola.text'"

A block of code is set as follows:

set y2tics -100, 10
set ytics nomirror
plot sin(1/x) axis x1y1,100*cos(x) axis x1y2

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

set samples 1000
set parametric
plot sin(7*t), cos(11*t) notitle

Any command-line input or output is written as follows:

plot 'randomnormal.text' volatile

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "We clicked on the tab Add plot commands to get the window."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.