Reader small image

You're reading from  gnuplot Cookbook

Product typeBook
Published inFeb 2012
PublisherPackt
ISBN-139781849517249
Edition1st Edition
Tools
Right arrow
Author (1)
Lee Phillips
Lee Phillips
author image
Lee Phillips

Lee Phillips grew up on the 17th floor of a public housing project on the Lower East Side of Manhattan. He attended Stuyvesant High School and Hampshire College, where he studied Physics, Mathematics, and Music. He received a Ph.D. in 1987 from Dartmouth in theoretical and computational physics for research in fluid dynamics. After completing post-doctoral work in plasma physics, Dr. Phillips was hired by the Naval Research Laboratory in Washington, DC, where he worked on various problems, including the NIKE laser fusion project. Dr. Phillips is now the Chief Scientist of the Alogus Research Corporation, which conducts research in the physical sciences and provides technology assessment for investors.
Read more about Lee Phillips

Right arrow

Chapter 7. Programming gnuplot and Dealing with Data

This chapter contains the following recipes:

  • Scripting gnuplot with its own language

  • Plotting on subintervals

  • Smoothing your data

  • Fitting functions to your data

  • Using kdensity smoothing to improve on histograms [new]

  • Creating a cumulative distribution [new]

  • Talking to gnuplot with C

  • Scripting gnuplot with Python

  • Plotting with Clojure

  • Handling volatile data [new]

Introduction


Up to now our style of interacting with gnuplot has mainly been to have an interactive conversation at the terminal, or to create a linear sequence of commands that can be read in and executed by typing gnuplot file. We went beyond this a bit in the previous chapter, where we learned how to arrange for a document preparation system to invoke gnuplot automatically. In this chapter, we shall learn how to leverage the full power of gnuplot as a programmable graphing engine. This is made possible by gnuplot's design as a program that can be completely controlled by textual commands, rather than by a limited graphical user interface. This means that any programming language that can send text to a process or socket can send commands to gnuplot. The result is that there are libraries for interfacing with gnuplot in almost every high-level programming or scripting language, and even in cases where no library is available it is straightforward to communicate manually from within your...

Scripting gnuplot with its own language


We have learned how to use a large number of gnuplot commands and assembled them into scripts, included with this book as code samples, that can be executed from the command line or read in from the gnuplot's interactive plot with the load command. Up to now, however, these scripts have taken the form of a simple sequence of commands, each one to be executed once; the same result is obtained whether we enter the commands one at a time at the prompt or load the script as a whole, in batch mode.

There are many tasks that are much more convenient, and others that are only possible, if we can apply some sort of automation to the generation of commands. Fortunately, gnuplot, as part of its command language, includes a simple facility for applying some basic programming constructs. There is a basic looping control, a way to iterate over a set of commands, and an if—then—else statement. With these few borrowings from procedural languages, we can do a great...

Plotting on subintervals


gnuplot has built-in a respectable collection of special functions and mathematical operators that allow us to perform significant calculations and massaging of data before plotting. A complete survey of gnuplot's math brain is beyond the scope of this book; interested readers should start with Chapter 13 of the official reference manual distributed with the program and available at http://gnuplot.info/documentation.html.

Here, we shall merely give an example of one very useful technique for plotting functions that have different definitions over different domains; this technique works equally well for functions that are continuous or discontinuous over the entire plotting domain. The example will demonstrate several of gnuplot's mathematical facilities that have not been covered up to now. Following figure is a plot of two functions, a simple sine wave that turns into a decaying sine wave when we cross x = 0:

How to do it…

The following script will produce the previous...

Smoothing your data


As an option to the plot command, gnuplot offers several smoothing functions. The name smooth is a bit misleading. Included in the options for the smooth plotstyle are several ways to process your data that would not most naturally be described as smoothing. We give examples of some of these in the following recipes. In the current recipe we show how to use the smoothing option that seems to be most immediately useful if you have some noisy data and want to draw a qualitatively smooth curve through it, to, as they say, guide the eye.

Getting ready

This recipe uses the datafile rs.dat; make sure that it is in your current directory. This file contains two columns that are the x and y coordinates of a simple sine wave of frequency and amplitude 1 to which normally distributed random data centered on 0 is added; that is, a sine curve plus the type of noise that might arise from actual measurements. In the following figure, we have a plot of the noisy sine wave, plotted with...

Fitting functions to your data


If we want to get serious about fitting functions to our data, we can turn to gnuplot's sophisticated fit command.

Getting ready

You need the same datafile used in the previous recipe, rs.dat. The following figure shows the same noisy sine wave, but this time the overlaid curve seems to be perfectly smooth:

How to do it…

The following script will produce the previous figure:

f(x) = a*sin(b*x)
fit f(x) 'rs.dat' via a, b
plot 'rs.dat' with lines lw 0.5 notitle, f(x) lw 4 title "Fit by gnuplot"

How it works…

gnuplot's fit command takes a function defined by the user containing several parameters and attempts to find the set of values for these parameters that result in the best fit of the resulting function to the data specified. Best fit is in a nonlinear least-squares sense that is thoroughly discussed in the official gnuplot manual. After entering the fit command, gnuplot will iterate, displaying intermediate results on the console, until it finds an acceptable fit...

Using kdensity smoothing to improve on histograms [new]


In Chapter 1, Plotting Curves, Boxes, Points, and more, we saw how to plot histograms, which are a type of statistical plot that show how a set of data is distributed: how many samples lie within each range of values, or bin. There are problems with histograms, venerable as they are, however. For example, the apparent shape of the distribution depends in part on how big we make our bins. Histogram plots can be misleading, and do not always serve the statistical purpose for which they were intended.

For these reasons and others, statisticians have invented other ways to calculate and display information about the distribution of a collection of data. One of these is called the kernel density estimate, which may be thought of as a kind of smoothed, bin-independent histogram. This is built into gnuplot as a plotting style—an option to the smooth style. This is a new feature in gnuplot version 4.4.

Getting ready

Make sure you are in a directory...

Creating a cumulative distribution [new]


In this recipe, we show how to use another useful statistical plot hiding away in the smoothing options, and also new to gnuplot in version 4.4. The curve plotted in the next figure is closely related to the one plotted in the previous recipe. To get the following figure, just integrate the frequency distribution curve:

Getting ready

Make sure you are in a directory with the supplied datafile called randomnormal.text.

How to do it…

The following script will get you the previous figure:

set key top left
plot 'randomnormal.text' using 1:(.001) smooth cumul \
      title "Cumulative distribution"

How it works…

The normalization issues are the same as the kdensity discussion in the previous recipe. The only difference here is the choice of smooth cumul—an abbreviation for cumulative. We can verify that we have a properly normalized curve because it approaches 1 as we approach the endpoint on the right. This is what we should expect, because this curve gives...

Talking to gnuplot with C


In this and the next few recipes, we are going to demonstrate how to use gnuplot from within different programming languages. There are no illustrations for these recipes, as we are not giving examples of particular types of plots. We provide samples of code for several languages that you can modify to get started immediately incorporating gnuplot's plotting facilities into your programs.

The first example was chosen to demonstrate that we can use gnuplot from within almost any programming language, even if no specialized library exists for that purpose.

Getting ready

You need to have a C compiler installed to follow along here. Just about any Unix-type operating system, including Linux and Macintosh (if the Developer Tools have been installed) comes with one, and it is usually called gcc.

How to do it…

Compile the following program with gcc file.c, where file.c is the name of the file in which it is saved (the example distributed with this book that is named after...

Scripting gnuplot with Python


In this recipe, we turn to the scripting language Python. This high-level language is well suited to the interactive exploration of data and the rapid development of programs for its analysis. This facility is made even stronger by its extension library numpy, which adds a rich set of efficient, C-coded routines for manipulating numerical arrays. In addition, there are several ways to make plots directly from Python. In this recipe we introduce Python's gnuplot interface, gnuplot.py.

Getting ready

You will need an installation of Python and its numerical library numpy, which comes with several popular scientific Python packages. You also need gnuplot.py, which you also may already have if you are doing scientific work with Python. If not, you can download it from its official Sourceforge site at http://gnuplot-py.sourceforge.net/.

How to do it…

Run the following code by typing python file.py, substituting the name of the Python script for file (the included sample...

Plotting with Clojure


Clojure is a dialect of lisp that is implemented on the Java Virtual Machine, so it is easy to install and run almost anywhere. The details can be found at http://clojure.org/. As this book is being prepared Clojure is rapidly becoming popular, as it is gaining recognition as perhaps the most practical implementation of an early high-level language that many consider to be the most powerful ever devised. Although Clojure is still in a somewhat early stage of adoption, it is quite mature, used in large projects and by big companies, and you can use gnuplot from it.

Getting ready

You need, naturally, an installation of the Clojure language. You may be able to install it using your package manager if you are using a Linux distribution (it is available in Ubuntu, for example); otherwise, you can download it from its official page at http://clojure.org/downloads. We are using the program clojure-gnuplot by Vadim Shender, so you should get that from https://bitbucket.org/vshender...

Handling volatile data [new]


Older versions of gnuplot ran into a problem when dealing, for example, with datafiles whose content is subject to change. When using gnuplot to examine such volatile data interactively, we might want to zoom in or out, or add a title to the plot to prepare it for saving. The problem is that each of these operations either requires an explicit call to replot or, in the case of mouse operations, calls it implicitly. And when the replot command is issued, the datafile is read anew, and the new data is plotted. What if we want to manipulate the plot while retaining the data already read in?

The new features in gnuplot version 4.4 for dealing with volatile data were added with these issues in mind.

How to do it…

The volatile data source

In order to play with this feature, it is convenient to have some volatile data available. If you run the following Python program, supplied as the code sample randomnormalIntervals.py, in the background, it will make a datafile and replace...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
gnuplot Cookbook
Published in: Feb 2012Publisher: PacktISBN-13: 9781849517249
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Phillips

Lee Phillips grew up on the 17th floor of a public housing project on the Lower East Side of Manhattan. He attended Stuyvesant High School and Hampshire College, where he studied Physics, Mathematics, and Music. He received a Ph.D. in 1987 from Dartmouth in theoretical and computational physics for research in fluid dynamics. After completing post-doctoral work in plasma physics, Dr. Phillips was hired by the Naval Research Laboratory in Washington, DC, where he worked on various problems, including the NIKE laser fusion project. Dr. Phillips is now the Chief Scientist of the Alogus Research Corporation, which conducts research in the physical sciences and provides technology assessment for investors.
Read more about Lee Phillips