tags:

views:

1173

answers:

5

Hi,

I am just about to find out how python and gnuplot work together. On

http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module

I found an introduction and I wanted to execute it on my Ubuntu machine.

import Gnuplot

gp = Gnuplot.Gnuplot(persist = 1)
gp('set data style lines')

data1 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]] # The first data set (a quadratic)
data2 = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]] # The second data set (a straight line)

plot1 = Gnuplot.PlotItems.Data(data1, with_="lines", title="Quadratic")
plot2 = Gnuplot.PlotItems.Data(data2, with_="points 3", title=None) # No title

gp.plot(plot1, plot2)

However, I get the following error message:

./demo.py
./demo.py: line 2: syntax error near unexpected token ('
./demo.py: line 2:
gp = Gnuplot.Gnuplot(persist = 1)'

any idea what could be wrong here? To install gnuplot support for python I installed python-gnuplot. Do I miss another package?

A: 

Well the python interpreter thinks that while parsing there was an syntax error. Check again your quotes, make sure that for convenience sake you only use either double or single quotes in your entire script (except of course when you need to put in a literal quote like "'" or '"').

If you are unsure what goes wrong what you can do is open the interactive interpreter and write each line in there.

Martin P. Hellwig
A: 

I've been using pylab instead. Pylab homepage

From debian repos:

python-matplotlib - Python based plotting system in a style similar to Matlab.
Macarse
A: 

Your demo.py file is somehow corrupted - make sure that the open-parenthesis character is really that. Grub the installer from the project page to make sure.

You can access the current SVN version of the file (choose download of the HEAD revision).

gimel
A: 

If I copy and paste your code into Emacs I get this:

gp = Gnuplot.Gnuplot(persist = 1)
 gp('set data style lines') 
data1 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]] # The first data set (a quadratic)
 data2 = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]] # The second data set (a straight line) 
plot1 = Gnuplot.PlotItems.Data(data1, with_="lines", title="Quadratic")
 plot2 = Gnuplot.PlotItems.Data(data2, with_="points 3", title=None) # No title 
gp.plot(plot1, plot2)

If I remove the whitespaces at the beginning of the three lines it works for me.

wr
+1  A: 

Did you put the bangline in the first line? i.e:

#!/usr/bin/python

Looks like it is not the Python interpreter who is executing the file.

kovan