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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s