Richard
Richard

Reputation:

gnuplot syntax error when using python

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

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

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


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?

Upvotes: 1

Views: 4154

Answers (5)

kovan
kovan

Reputation: 770

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.

Upvotes: 2

wr.
wr.

Reputation: 2859

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.

Upvotes: 0

gimel
gimel

Reputation: 86344

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

Upvotes: 0

Macarse
Macarse

Reputation: 93133

I've been using pylab instead. Pylab homepage

From debian repos:

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

Upvotes: -1

Martin P. Hellwig
Martin P. Hellwig

Reputation: 730

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.

Upvotes: 0

Related Questions