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 is called Scripting_gnuplot_with_Python.py):

from numpy import *
import Gnuplot
g = Gnuplot.Gnuplot()
g.title('My Favorite Plot')
g.xlabel('x')
g.ylabel('sin(1/x)')
g('set term pngcairo')
g('set out "GnuplotFromPython.png"')
g('plot sin(1/x)')

You should then have a PNG file with your plot in your current directory.

How it works…

After importing the Gnuplot package and setting a variable equal to the function Gnuplot.Gnuplot(), we can issue commands directly to gnuplot just by passing strings to g. The details of starting the process and opening the pipe, that we did manually in the previous recipe, are taken care of for us automatically by the gnuplot.py package. The package also contains many abstracted gnuplot commands, such as the g.title() function shown in the example. Read the demo files and documentation that come with the gnuplot.py download to find out everything else it can do.

There's more…

The previous example is a bit contrived; if all you could do with the Python-gnuplot interface was to send commands that you could just as easily issue directly from the gnuplot interactive prompt, it would be of limited utility. The real value of this package is its ability to plot arrays calculated from within our Python program. The following program is included as the file Scripting_gnuplot_with_Python_2.py:

from numpy import *
import Gnuplot
g = Gnuplot.Gnuplot()
g.title('Normally Distributed Random Data')
g.xlabel('x')
g.ylabel('y')
g('set term pngcairo')
g('set out "GnuplotFromPython2.png"')
x = arange(1, 1000)
y = [random.normal() for i in x]
g.plot(Gnuplot.Data(x, y))

In the last line, we've used another abstraction provided by the gnuplot.py package, Gnuplot.Gnuplot().plot(), combined with the Gnuplot.Data() object, to plot one python numpy array (or list cast to an array) versus another. In fact, the array stored in y uses the random number generator that we probed in the previous recipes introducing the smooth kdensity and smooth cumulative plot commands.

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

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