[matplotlib-devel] File format for plots

2009-02-25 Thread sam tygier

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


Re: [matplotlib-devel] File format for plots

2009-02-25 Thread Troels Kofoed Jacobsen
On Wednesday 25 February 2009 09:35:07 am sam tygier wrote:
> 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

I think this is a good idea, but why don't you just save your data to a file 
and plot from a different script. If the data is only numbers you can just do 
savetxt('data.dat',data) in you simulation script and then 
data=loadtxt('data.dat') from your plot script...
Now if you also just use savefig('fig') without suffix, you can just run your 
plot script like: python plot.py -DAgg or -DPS or whatever and it will plot to 
the default format for that backend.

Best regards
Troels Kofoed Jacobsen

--
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


Re: [matplotlib-devel] plot([], [], drawstyle = 'steps') fails

2009-02-25 Thread Michael Droettboom
This is now fixed in SVN.  Thanks for the report.

Mike

Gregor Thalhammer wrote:
> Hi all,
>
> sorry, only reporting, no bugfix. I just discovered that an empty plot 
> with drawstyle 'steps', 'steps-pre', 'steps-mid' and 'steps-post' fails. 
> I am using matplotlib 0.98.5.2.
>
> Example
>
> plot([], [], drawstyle = 'steps')
>
> ...
> C:\Python25\lib\site-packages\matplotlib\lines.pyc in 
> _draw_steps_pre(self, renderer, gc, path, trans)
> 784 def _draw_steps_pre(self, renderer, gc, path, trans):
> 785 vertices = self._xy
> --> 786 steps = ma.zeros((2*len(vertices)-1, 2), np.float_)
> 787
> 788 steps[0::2, 0], steps[1::2, 0] = vertices[:, 0], 
> vertices[:-1, 0
> ]
> ...
>
> ValueError: negative dimensions are not allowed
>
> Gregor
>
>
>
>
>
> --
> 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
>   

-- 
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA


--
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


Re: [matplotlib-devel] File format for plots

2009-02-25 Thread sam tygier
Troels Kofoed Jacobsen wrote:
> On Wednesday 25 February 2009 09:35:07 am sam tygier wrote:
>> 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.
> 
> I think this is a good idea, but why don't you just save your data to a file 
> and plot from a different script. If the data is only numbers you can just do 
> savetxt('data.dat',data) in you simulation script and then 
> data=loadtxt('data.dat') from your plot script...
> Now if you also just use savefig('fig') without suffix, you can just run your 
> plot script like: python plot.py -DAgg or -DPS or whatever and it will plot 
> to 
> the default format for that backend.
> 
> Best regards
> Troels Kofoed Jacobsen

That is one method that i have used, but i don't think it is ideal. My data can 
be a wide range of things, sometimes the coordinates of a bunch of many 
particles, sometimes the track of one. If I save just an array of numbers it 
can get a bit confusing. So it would be useful to be able to save everything 
needed to make the plot.

Sam Tygier


--
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