I think this topic has come up before, but i don't think anything has resulted 
from it.

I'd like a way for saving a plot from from matplotlib, so that it can be 
re-rendered later, possibly with a different backend, maybe to a different 
size, and maybe with changes to the labels. This would save me having to rerun 
the simulation that generated the plot.

Ideally this would work by having a save_plot() function, that would save all 
state of the current plot into a file. This could then be loaded by a program 
to regenerate that plot.

I have made a rough prototype to demonstrate. It is incomplete. It only 
implements a very small subset of pylab.

I shall attach some files (if these get mangled, then i can upload them 
somewhere).

example1 and example2 are what the plot files might look like.
plot.py renders the plot files.
eg.
plot.py example1
plot.py example2 example.png

fakepylab.py is a wrapper around pylab that record you plotting, and offers a 
save_plot() function

test.py is script that uses fakepylab to create a plot file.

So does any of this look useful? What more might it need to be useful?

Any comments on the file format. Is there an existing standard that could be 
used instead?  Would XML be better than plain ascii?

Sam Tygier

title example plot
xlabel displacemnt / m
ylabel time / s
data x
1
2
3
4
5
6
7
8
9
10
enddata
data y
2.1
2.3
2.7
3.1
3.6
4.2
5.0
5.7
6.7
7.4
enddata
plot x y

data x
1
2
3
4
5
6
7
8
9
10
enddata
data y
2.1
2.3
2.7
3.1
3.6
4.2
5.0
5.7
6.7
7.4
enddata
data z
5.1
6.3
7.7
8.1
9.6
8.2
7.0
6.7
5.7
4.4
enddata
subplot 121
title example plot
xlabel displacemnt / m
ylabel time / s
plot x y

subplot 122
title second plot
xlabel displacemnt / m
ylabel time / s
plot x z

#!/usr/bin/env python

import pylab as _pylab

for pylab_item in dir(_pylab):
	globals()[pylab_item] = eval("_pylab.%s"%pylab_item)

_output_buffer = []

def title(t):
	_output_buffer.append("title %s"%t)
	_pylab.title(t)

def xlabel(t):
	_output_buffer.append("xlabel %s"%t)
	_pylab.xlabel(t)

def ylabel(t):
	_output_buffer.append("ylabel %s"%t)
	_pylab.ylabel(t)

def subplot(t):
	_output_buffer.append("subplot %s"%t)
	_pylab.subplot(t)

def plot(x, y):
	_output_buffer.append("data x")
	for point in x:
		_output_buffer.append(str(point))
	_output_buffer.append("enddata")

	_output_buffer.append("data y")
	for point in y:
		_output_buffer.append(str(point))
	_output_buffer.append("enddata")

	_output_buffer.append("plot x y")
	 
	_pylab.plot(x, y)

def save_plot(fname):
	global _output_buffer
	fh = open(fname,'w')
	fh.write("\n".join(_output_buffer))
	fh.write('\n')
	fh.close()
	_output_buffer = []


#!/usr/bin/env python
import sys
import numpy

infile = open(sys.argv[1])

try:
	outfilename = sys.argv[2]
	outmode = "file"
except IndexError:
	outmode = "screen"
	outfilename = None

if outmode == "file":
	if outfilename.endswith('.ps') or outfilename.endswith('.eps'):
		import matplotlib
		matplotlib.use('ps')
	if outfilename.endswith('.pdf'):
		import matplotlib
		matplotlib.use('pdf')
	if outfilename.endswith('.png'):
		import matplotlib
		matplotlib.use('Agg')


import pylab
plot_param = dict(title='', xlabel='', ylabel='')
plot_data = dict()

infilei = (line.strip() for line in infile if line.strip() != '' and not line.startswith('#'))

for line in infilei:
	keyword, dummy, args = line.partition(' ')

	if keyword in ['title', 'xlabel', 'ylabel']:
		plot_param[keyword] = args
		continue
	
	if keyword == "data":
		data = []
		dataname = args.split()[0]
		for dataline in infilei:
			if dataline == "enddata":
				break
			data.append(float(dataline))
		plot_data[dataname] = numpy.array(data)
		continue

	if keyword == "subplot":
		plot_param = dict(title='', xlabel='', ylabel='')
		pylab.subplot(args)


	if keyword == "plot":
		x, y = args.split()
		pylab.title(plot_param['title'])
		pylab.xlabel(plot_param['xlabel'])
		pylab.ylabel(plot_param['ylabel'])
		pylab.plot(plot_data[x], plot_data[y])
		continue

	print "unhandled line:", line


if outmode == "screen":
	pylab.show()
else:
	pylab.savefig(outfilename)



#!/usr/bin/env python

import fakepylab as p
p.title("hello")
p.plot([1,2,3,4],[2,3,2,1])
p.show()
p.save_plot("test")

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to