Python & Gnuplot

Gnuplot is a portable command-line driven graphing utility for Linux, OS/2, MS Windows, OSX, VMS, and many other platforms. The source code is copyrighted but freely distributed (i.e., you don’t have to pay for it). It was originally created to allow scientists and students to visualize mathematical functions and data interactively, but has grown to support many non-interactive uses such as web scripting

Gnuplot can be opened as a subprocess within a Python script.

The follow lines of code are required for the case of the sample program running in Windows or Linux:

from subprocess import Popen, PIPE

import platform

if platform.system() == ‘Windows’ or platform.system() == ‘Linux’:

if platform.system() == ‘Windows’:

gnuplot = r’C:\Program Files (x86)\gnuplot\bin\pgnuplot’
plot=Popen([gnuplot,’-persist’],stdin=PIPE,stdout=PIPE,stderr=PIPE)
if platform.system() == ‘Linux’:

plot=Popen([‘gnuplot’,’-persist’],stdin=PIPE,stdout=PIPE,stderr=PIPE)
# do some plot commands common to Windows & Linux

Here is the sample script which generates a gnuplot file, saves it to a folder, then loads it into gnuplot.

Windows & Linux version: python_gnuplot_demo.py
Here is the data file used in the demonstration: drop.txt

See also:

Gnuplot Blog Entry

Talking to gnuplot by pipes

Tom Irvine

Advertisements