Python:Gnuplot module

From AIMSWiki

Table of contents

Basic plotting

This is a simple example of how to plot data using the Gnuplot module.

import Gnuplot

gp = Gnuplot.Gnuplot(persist = 1) 

By using the persist=1 command, you can make persistent gnuplot windows; meaning that such windows stay around even after the gnuplot program is exited. This is particularly useful when you are plotting several graphs in your Python code. Using the debug=1 option creates a new plot window and closes the previous one. Note that only newer versions of gnuplot support this option.

gp('set data style lines')

data = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]]    # Pairs of x and y values
gp.plot(data)

Note that the data has to be a list of pairs of values representing x and y coordinates.

 Commands to plot

There are two basic commands to plot a graph, plot and splot . The former is used for a 2-dimensional graph, and the latter is for a 3-dim. Gnuplot makes a graph of any kinds of functions or numerical data stored in a file. To plot a function, use the plot/splot command with a range of X-axis (or X and Y ranges for 3-dim. plot) and the function. You can omit the range parameters. Here is an example of plotting y=sin(x), which you may often see at many gnuplot tutorials.

gnuplot> plot sin(x)

basic1

Gnuplot commands

You can execute any Gnuplot command directly using

gp('command')

as with the gp('set data style lines') command above. For example, to increase the default point size to 3:

gp('set pointsize 3')

Multiple plots: styles and titles

If you want to plot different graphs with different options (like line style and title) you should use the following:

import Gnuplot

gp = Gnuplot.Gnuplot(persist = 1)
gp('set data style lines')
 
data1 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]]    # The first data set (a quadratic)
data2 = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]     # The second data set (a straight line)

# Make the plot items
plot1 = Gnuplot.PlotItems.Data(data1, with_="lines", title="Quadratic")
plot2 = Gnuplot.PlotItems.Data(data2, with_="points 3", title=None)  # No title

gp.plot(plot1, plot2)

This draws two plots on the same figure. The first graph will be drawn with the lines style and have the title Quadratic, the second graph will be drawn using points only and without a title.

Vector fields

Vector fields are drawn using 4 column data [[x0, y0, u0, v0], [x1, y1, u1, v1], ... ]. Each (xi,yi) specifies a point in the plane and each (ui,vi) the vector drawn at that point.

# Initialize gnuplot
import Gnuplot
gp = Gnuplot.Gnuplot(persist = 1)

# Generate the vector field data
data = []
for i in range(-10, 11):
   for j in range(-10, 11):
      data.append([i/10., j/10., -i/100., -j/100.]) # Each data entry has the form [x, y, u, v]

# Plot everything: note that the with argument is 'vectors'
plot = Gnuplot.PlotItems.Data(data, with_ = 'vectors')
gp.plot(plot)

Your output should look like this:

The plot for this code


Here is another example:

 # Initialize gnuplot
 import Gnuplot
 import math
 gp = Gnuplot.Gnuplot(persist = 1)
 # Generate the vector field data
 data = []
 for i in range(-10, 11):
    for j in range(-10, 11):
       data.append([i/100., math.sin(j)/100., -i/10., -math.cos(j)/10.]) # Each data entry has the form [x, y, u, v]
 # Plot everything: note that the with argument is 'vectors'
 plot = Gnuplot.PlotItems.Data(data, with_ = 'vectors')
 gp.plot(plot)

You can write script in a text editor and save it as a ".dat" file. Once this is done, you load it from the gnuplot terminal. An example script titled "demo.dat" is shown below:

set view map
set isosamples 31, 31
unset surface
set contour base
set cntrparam levels discrete -3,-2 ,-1 ,-0.5 ,-0.2 ,-0.1 ,-0.05 ,-0.02 ,0 ,0.02 ,0.05 ,0.1 ,0.2 ,0.5 ,1 ,2 ,3 
set rrange [ 0.00000 : 10.0000 ] noreverse nowriteback
set trange [ -5.00000 : 5.00000 ] noreverse nowriteback
set urange [ -5.00000 : 5.00000 ] noreverse nowriteback
set vrange [ -5.00000 : 5.00000 ] noreverse nowriteback
set xrange [ -10.0000 : 10.0000 ] noreverse nowriteback
set x2range [ -10.0000 : 10.0000 ] noreverse nowriteback
set yrange [ -10.0000 : 10.0000 ] noreverse nowriteback
set y2range [ -10.0000 : 10.0000 ] noreverse nowriteback
set zrange [ -10.0000 : 10.0000 ] noreverse nowriteback
set cbrange [ -10.0000 : 10.0000 ] noreverse nowriteback
r(x,y)=sqrt(x*x+y*y)
v1(x,y)=  q1/(r((x-x0),y))
v2(x,y)=  q2/(r((x+x0),y))
vtot(x,y)=v1(x,y)+v2(x,y)
e1x(x,y)= q1*(x-x0)/r(x-x0,y)**3
e1y(x,y)= q1*(y)/r(x-x0,y)**3
e2x(x,y)= q2*(x+x0)/r(x+x0,y)**3
e2y(x,y)= q2*(y)/r(x+x0,y)**3
etotx(x,y)=e1x(x,y)+e2x(x,y)
etoty(x,y)=e1y(x,y)+e2y(x,y)
enorm(x,y)=sqrt(etotx(x,y)*etotx(x,y)+etoty(x,y)*etoty(x,y))
dx1(x,y)=coef*etotx(x,y)/enorm(x,y)
dy1(x,y)=coef*etoty(x,y)/enorm(x,y)
dx2(x,y)=coef*etotx(x,y)
dy2(x,y)=coef*etoty(x,y)
q1 = 1
x0 = 1.0
q2 = -1
coef = 0.7
xmin = -10.0
xmax = 10.0
ymin = -10.0
ymax = 10.0
splot vtot(x,y) w l

Your output should be similar to this:

The plot for this code

Printing Graphs

You can print out graphs of functions plotted directly from your Python code by using the gp.hardcopy() command. This uses the default ! lpr command to produce the output. However, if you would like to print your output to a postscript file, you need to specify the filename of your output. For example, the plot for the Vector field above can be saved to a file by adding the following line below the gp.plot command as follows:

gp.hardcopy(filename="Vector field.ps",terminal="postscript") 

The output is automatically saved to the default location of your saved files which could either be your Home folder or your Desktop. The .ps file created is a temporary one and is replaced/updated everytime you re-run your program. You can learn some more in-built functions by typing in help(Gnuplot.Gnuplot) in the command line mode of Python.


Return to the Python index

Taking a Desktop Screen Shot

On the Keyboard press the button written PrintScreen/SysRq.