posted on: 2008-07-04 12:24:08
This started as a command line script, and I made a class that I use with other scripts for producing large numbers of graphs quickly.

>The only requirements for this are python 2.3 and, of course, you must have gnuplot. I do not intend on doing too much development with this because the is an existing gnuplot.py. What I have done is merely so that I can type from the command line.

clgnuplot.py data.txt

Or if I want to plot a bunch of files.

clgnuplot.py data*

Here is the code.

#!/usr/bin/env python
"""
Gnuplot utility, plot a datafile from command line with data seperated into columns
"""
import sys,os
import subprocess

Ghelp = """
Any options must follow the specific form of '-o value'

These first commands a implemented in sequence of being called.

	-x 	choose column for x data
	-y	choose column for y data
	-s	set the line type for any data set specified after ward
	
These options are called before the plot is made, so every dataset will be affected

	-f	include a function it wil use the current style
	-o	directly sent as a line to the gnuplot interpreter.
	-p 	out put to a ps file you need the filname option
	"""

class Gs(object):
	def __init__(self):
		self.refresh()
		self.moddict = {	'x':self.setx,
					'y':self.sety,
					's':self.setstyle,
					'f':self.function,
					'o':self.rawoption,
					'p':self.wfile
		}
		self.start()
	
	def __call__(self,x,y):
		self.moddict.get(x,self.nonmod)(y)
	def setx(self,x):
		self.xdata = x
	def sety(self,x):
		self.ydata = x
	def setstyle(self,x):
		self.style = ' with %s'%x
	def nonmod(self,x):
		print 'the -%s %s is not a valid option'%(mod,x)
	def function(self,x):
		self.data += self.sep + "%s %s"%(x,self.style)
		self.sep = ', '
	def rawoption(self,x):
		self.options.append('%s\n'%x)
	def wfile(self,x='graph.ps'):
		self.options.append('set term postscript color\n')
		self.options.append('set out "%s"\n'%x)
		self.pause = False
	def datafile(self,x):
		self.data += self.sep + "'%s' using %s:%s%s"%(x,self.xdata,self.ydata,self.style)
		self.sep = ', '
	
	#executes the options and then the plot cmd
	def plot(self):
		self.data += "\n"
		for thing in self.options:
			self.program.stdin.write(thing)
		self.program.stdin.write(self.data)
		if self.pause==True:
			text = raw_input()
		self.program.stdin.write('\n')
	
	#cleans up
	def end(self):
		(callSTD,callErr) = self.program.communicate()
	
	#starts the program
	def start(self):
		exec_args = ['/usr/bin/gnuplot']
		popenarg = {'stdout':subprocess.PIPE,'stdin':subprocess.PIPE}
		self.program = subprocess.Popen(exec_args,**popenarg)
	
	#initializes a new plot statement.
	def refresh(self):
		self.xdata = '1'
		self.ydata = '2'
		self.style = ''
		self.data = "plot "
		self.sep = ''
		self.pause = True
		self.options = []


#Command line routine
if __name__=='__main__':
	if len(sys.argv)>1:
		G = Gs()
		filecoming = 1
		for item in sys.argv[1:]:
			if filecoming==1:
				if item[0]!='-':
					G.datafile(item)
				else:
					mod = item[1]
					filecoming=0
			else:
				G(mod,item)
				filecoming=1
		G.plot()
		G.end()
	else:
		print Ghelp

Comments

Matt
2008-07-01 15:32:39
This could probably be done with a pipe instead of a temporary file. I might switch that since it wouldn't change the functionality, but it would stop the accumulation of temporary files in my tmp directory.
matt
2008-07-02 12:26:40
This version is outdated already. It doesn't process options for one thing and it pauses when you write a file, both of which have been fixed already.
Name: