About this Book

This book is intended to be a comprehensive introduction to gnuplot: from the basics to the power features and beyond. In addition to providing a tutorial on gnuplot itself, it demonstrates how to apply and use gnuplot to extract insight from data.

The gnuplot program has always had complete and detailed reference documentation, but what was often missing was a continuous presentation that tied all the different bits and pieces of gnuplot together and demonstrated how to use them to achieve certain tasks. This book attempts to fill that gap. It should also serve as a handy reference for more advanced gnuplot users and as an introduction to graphical ways of knowledge discovery.

And finally, this book tries to show you how to use gnuplot to achieve some surprisingly nifty effects that will make everyone say, “How did you do that?”

Contents of this book

This book is divided into four parts. Part 1 consists of chapters 1 through 3 and is intended as a tutorial introduction to get you started with gnuplot. These three chapters cover all the truly essential material so that by the end of chapter 3, you should be able to handle most basic plotting tasks in gnuplot.

Whereas part 1 only skims the surface, part 2 goes into depth. First, chapters 4 and 5 lay more groundwork by talking about the ins and outs of file formats, string handling, and other practical matters. Then, chapters 6 through 8 discuss the various ways to change the appearance of a plot: using different plotting styles; adding labels, arrows, or other decorations; and changing the axes and their subdivisions. These chapters cover the tactical aspects of working with gnuplot in detail.

Part 3 turns its attention away from individual graphs and addresses a variety of more technical aspects. First, in chapter 9, you’ll learn more about color specification, point and line types, and other relatively low-level graph elements. Chapter 10 explains how to export plots to common graphics file formats. Finally, chapters 11 and 12 address ways to improve the overall workflow through scripting and configuration changes.

In the last part, I’ll mostly take gnuplot’s features for granted and concentrate on the things you can do with them. Chapter 13 presents various fundamental types of graphs and explains when and how to use them. Chapter 14 is more advanced and offers solutions to some recurring topics in graphical analysis, before we end the book with a reminder of what it’s all about in chapter 15.

The book has several appendixes. Appendix A explains how to obtain, build, and install gnuplot. Appendix B provides pointers to some relevant resources.

Finally, some topics of a more specialized character have been relegated to a set of supplemental appendixes: appendixes C and D discuss three-dimensional surface plots and false-color plots (heatmaps), appendix E treats some special types of graphs, and appendix F covers more mathematical topics. To reduce the physical dimensions of the print book, these four appendixes are only available in the electronic (e-book) version of this book. The purchase of a hard copy includes access to the e-book as well—you can find instructions in the front of the print book.

Tip

Appendixes C through F are only available in the e-book version of this book, which is included with the purchase of the hard-copy version. Check the front of the print book for instructions on how to obtain the e-book.

How to read this book

This book was written as if readers were going to read it sequentially, cover to cover. New material is presented in order, with later chapters relying only on topics introduced earlier and avoiding forward references as much as possible. I realize that this is not a realistic picture and that the need for technical information tends to arise in a much more disjointed manner. In this spirit, I offer a few different “trail maps” to the material presented here:

  • If you’re new to gnuplot, begin with chapters 2 and 3 and then dive into chapters 48 as required to pick up the skills you need to complete whatever task you want to accomplish.
  • If you’re comfortable creating day-to-day graphs with gnuplot, then the material in chapters 912 should help you achieve greater efficiency in your work and fine-tune the results.
  • If you’ve been using gnuplot for a long time already, then make sure you read up on the new features in gnuplot 5. Chapters 5, 9, and 10, as well as parts of chapters 11 and 12 will probably be of the most immediate interest to you.
  • If you’re new to graphical analysis, you may want to begin with chapter 13 to learn some of the basic methods and concepts.

Finally, keep in mind that some interesting and useful material is only available in the e-book. Three-dimensional surface and contour plots are discussed in appendix C. False-color plots (heatmaps) are treated in appendix D, together with guidelines for how to construct effective color gradients for data visualization. Appendix E explains how to combine individual graphs into composites and also discusses some other specialized types of graphs. Appendix F treats topics of a more mathematical nature.

Whom this book is for

This book is intended for anyone who wants to plot and visualize data, either to explore data sets graphically, or to create attractive, high-quality graphs for presentation and publication purposes. I had two kinds of people in mind when writing this book—those who already know gnuplot, and those who don’t:

  • If you already know gnuplot, I hope you’ll still find it a useful reference, in particular in regard to some of the more advanced topics later in the book. I’ve tried to provide exactly the big-picture explanations and examples that have always been missing from the standard gnuplot reference documentation.
  • If you’re new to gnuplot, I think you’ll find it easy enough to pick up—in fact, I can promise you that by the end of chapter 2, you’ll be productive with gnuplot; and by the end of chapter 3, you’ll be well equipped for most day-to-day data graphing tasks that may come your way.

This book doesn’t require a strong background in mathematical methods or any in statistics, but I occasionally do expect you to have at least a fleeting familiarity with simple programming concepts. A few sections naturally require some special preliminaries (for instance, some of the discussions in chapter 10 require knowledge of LaTeX, and some sections in chapter 11 use Perl or Python code), but you can safely skip those sections if their material doesn’t apply to you.

Conventions

I spell the name of the program in all lowercase (gnuplot), except at the beginning of a sentence, when I capitalize it normally. This is in accordance with the usage recommended in the gnuplot FAQ.

The gnuplot documentation is extensive, and I refer to it occasionally for additional details on topics covered only briefly or not at all here. Traditionally, the gnuplot documentation has been called the online help or online documentation, owing to the fact that it’s available online during a gnuplot session. But since the advent of the internet, the word online seems to suggest network connectivity—falsely, in this context. To avoid confusion, I’ll always refer to it as the standard gnuplot reference documentation.

Code examples

Gnuplot commands are shown using a monospace font, like this: plot sin(x). Gnuplot commands can be entered at the gnuplot command prompt as shown in the text; the prompt itself has been suppressed to save space.

Single command lines can be long; to make them fit on a page, I occasionally had to break them across multiple lines. If so, a gray arrow () has been placed at the beginning of the next line, to indicate that it is the continuation of the previous one:

plot "data" using 1:2 smooth csplines title "data" with lines,
   sin(x) title "model"

The break in the original line isn’t indicated separately. When using gnuplot in an interactive session, your terminal program should automatically wrap a line that’s too long. Alternatively, you can break lines by escaping the newline with a backslash as usual. This is useful in command files for batch processing (and you’ll see some examples in chapter 12 in the context of string macros).

Some code snippets are only intended to demonstrate the syntax and don’t have a graph associated with them. In this case, I use the generic name “data” as a placeholder for the actual filename. No file named data exists in the downloads (in the same way that no key named any can be found on a computer keyboard). It’s just a generic placeholder.

Occasionally, I show Unix commands that need to be entered in a Unix shell; to emphasize that these aren’t gnuplot commands, I prefix them with a generic shell prompt, like this: shell>. Similarly, Python commands to be entered in a Python session are prefixed with python>>>.

Downloads

The code for all numbered listings is available for download from www.manning.com/books/gnuplot-in-action-second-edition, and so are the data sets. The only exception to this are publicly available data sets: for these, I provide the URL where they can be found.

Gnuplot searches for data files in the current directory, so the easiest way to run the supplied command files is as follows:

1.  Change into the data directory of the downloaded bundle.

2.  Start gnuplot.

3.  Issue plot commands at the gnuplot prompt the way they’re shown in the text (for example, plot "marathon" using 1:2), or give the full pathname to the gnuplot command file that you wish to run (for example, load "../gnuplot/shapes.gp").

Command synopses

Gnuplot has a large number of options, and keeping all of them and their sub-options and optional parameters straight is a major theme running through this book. Frequently, I’ll display all available options to a command in a command synopsis before discussing the options in detail. To distinguish a synopsis of available options from actual gnuplot code, a synopsis uses an italic font, like so:

set datafile commentschar ["{str:chars}"]

Within these summaries, I use a few syntactic conventions. My intent here is to stay close to the usage familiar from the standard gnuplot reference documentation, but also to follow more general conventions (such as those used for Unix man pages):

For parameters supplied by the user, it’s often not clear from the context what kind of information the command expects: is it a string or a number? If it’s a number, is it a value selected from a fixed range of integers or a numerical factor? And so on. I’ve tried to clarify this situation by prefixing each user-supplied input parameter with a type indicator, terminated by a colon. I summarize the prefixes and their meanings in table 1.

Table 1. Type indicators for user-supplied parameters

Prefix

Description

str: A string
int: An integer number
flt: A floating-point number
idx: An integer number, which is interpreted as a selection from a fixed range of values
clr: A color specification—for example, rgbcolor "red" or rgb "#FFFF00"
pos: A pair of comma-separated coordinates, optionally containing coordinate system specifiers—for example, 0,0 or first 1.1, screen 0.9
enum: A gnuplot keyword as unquoted string

Abbreviations

Many gnuplot commands have abbreviated forms, which I use frequently. The essential plot command, in particular, takes a large number of keyword directives, which I usually abbreviate to save space and keystrokes. I strongly recommend that you quickly become familiar with these shorthands and use them yourself. Table 2 lists both the abbreviated and the full forms. The plot command also understands a large number of appearance options (controlling aspects such as line width, style, and color), which are generally also abbreviated. A comprehensive summary of appearance options, together with their shorthands, can be found in table 6.1.

Table 2. Abbreviations for frequently used directives to the plot command

Abbreviation

Full

i index
ev every
u using
s smooth
s acs smooth acsplines
s f smooth frequency
s kdens smooth kdensity
t title
w with
w l with lines
w linesp or w lp with linespoints
w p with points
w vec with vectors

Table 3 lists three frequently occurring commands that are also usually abbreviated.

Table 3. Abbreviations for frequently occurring commands

Abbreviation

Full

set t set terminal
set o set output
set logsc set logscale

The figures in this book

The graphs in this book were generated with gnuplot; some special cases were handled using pic. All graphs were originally prepared in color, using my own set of preferred colors instead of one of gnuplot’s default color schemes. The color versions of the graphs are used in the electronic (e-book) version of this book. For the print book, I prepared black-and-white versions through the application of an appropriate stylesheet (see chapter 12). A handful of graphs required manual touch-ups in addition to the monochrome stylesheet to yield an optimal appearance.

You’ll find the line-type definitions of both the color and the black-and-white stylesheets in table 4. The same colors and dash patterns are discussed in listings 12.7 and 12.9.

In particular in the latter part of the book, I frequently use point types (point shapes) that aren’t the default, because the visual appearance of the graphs can often be improved greatly this way. If so, the point type is usually chosen explicitly in the appropriate code examples and listings.

The final version of each figure was generated using the pdfcairo terminal, using a (non-default) aspect ratio of √2 to 1 and Helvetica as the requested font.

Table 4. Colors and dash patterns used for the color and monochrome figures in this book

Color

Monochrome

set linetype 1 lc rgb '0xee0000' set linetype 1 lc black dt solid
set linetype 2 lc rgb '0x008b00' set linetype 2 lc black dt (8,6)
set linetype 3 lc rgb '0x0000cd' set linetype 3 lc black dt (4,3)
set linetype 4 lc rgb '0xff3fb3' set linetype 4 lc black dt (3,6)
set linetype 5 lc rgb '0x00cdcd' set linetype 5 lc black dt (12,5,2,5,2,5)
set linetype 6 lc rgb '0xcd9b1d' set linetype 6 lc black dt (16,8)
set linetype 7 lc rgb '0x8968cd' set linetype 7 lc black dt (20,6,2,6)
set linetype 8 lc rgb '0x8b8b83' set linetype 8 lc black dt (30,10)

Hardware and software requirements

This book describes gnuplot version 5.0 or higher, which was initially released in early 2015. Not all examples in this book will work with earlier gnuplot versions. If you have an earlier version of gnuplot, you should upgrade to a more current version—appendix A tells you how.

I assume you have access to a reasonably modern computer running any flavor of Unix/Linux, a recent release of MS Windows, or Mac OS X. Gnuplot has been ported to many other platforms but is actively supported primarily on the three operating systems just mentioned, and so I concentrate on them in this book.

Reference materials

Command and option references are distributed throughout the book, wherever the material is first introduced. The following pointers are intended to help you find these summaries more easily.

Graphical styles and specifications

Appearance specifiers and line options Table 6.1 page 104
Graph locations Figure 7.2 page 128
Graph layers Figure 7.3 page 129
Explicit color-specification formats Table 9.1 page 184
Point types Figure 9.7 page 196
Dash patterns Table 9.2 page 197

File access

Column-access methods and functions Table 4.2 page 67
Pseudofiles Table 4.3 page 71
Metadata in data files Table 4.4 page 76

String handling and formatting

String functions Table 5.1 page 81
General conversion specifiers Table 8.2 page 160
Accuracy specifiers Table 8.3 page 160
Time-series conversions, sorted alphabetically Table 8.4 page 172
Time-series conversions, sorted by topic Table 8.5 page 173
Time functions Table 8.6 page 176
Enhanced text mode Table 10.1 page 218

Operators and mathematical functions

Unary operators Table 3.1 page 35
Binary operators Table 3.2 page 35
Mathematical functions Table F.1 page F11
Complex numbers Table F.2 page F13

Programming constructs

Inline loops Listing 5.3 page 92
General loops and conditionals Table 11.1 page 238

About the author

PHILIPP K. JANERT was born and raised in Germany. He obtained a Ph.D. in theoretical physics from the University of Washington in 1997 and has been working in the tech industry ever since, including four years at Amazon.com, where he initiated and led several projects to improve Amazon’s order-fulfillment process. He’s the author of several books on data analysis and applied math, including the best-selling Data Analysis with Open Source Tools (O’Reilly, 2010). He has contributed to CPAN and is an occasional committer on the gnuplot project. Visit his company website at www.principal-value.com.

Author Online

Purchase of Gnuplot in Action, Second Edition includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the lead author and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/books/gnuplot-in-action-second-edition. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to Author Online remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray! The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the cover

The figure on the cover of Gnuplot in Action, Second Edition is captioned “A peer of France.” The title of Peer in France was held by the highest-ranking members of the French nobility. It was an extraordinary honor granted only to a few dukes, counts, and princes of the church. The illustration is taken from a 19th-century edition of Sylvain Maréchal’s four-volume compendium of regional dress customs published in France. Each illustration is finely drawn and colored by hand.

The rich variety of Maréchal’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.

Dress codes have changed since then, and the diversity by region, so rich at the time, has faded away. It’s now hard to tell apart the inhabitants of different continents, let along different towns or regions. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.

At a time when it’s hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Maréchal’s pictures.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.88.54