使用python时gnuplot语法错误

时间:2009-06-24 12:00:35

标签: python gnuplot

我即将了解python和gnuplot如何协同工作。在

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

我找到了一个介绍,我想在我的Ubuntu机器上执行它。

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) 

但是,我收到以下错误消息:

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

知道这里有什么不对吗?为python安装gnuplot支持我安装了python-gnuplot。我想念另一个包吗?

5 个答案:

答案 0 :(得分:2)

你把bangline放在第一行吗?即:

#!/usr/bin/python

看起来不是Python解释器正在执行该文件。

答案 1 :(得分:0)

嗯,python解释器认为解析时存在语法错误。 再次检查你的报价,确保为方便起见,你只在整个脚本中使用双引号或单引号(当然,当你需要输入像“'”或“”这样的文字引号时)。

如果您不确定出了什么问题,您可以打开交互式解释器并在其中写下每一行。

答案 2 :(得分:0)

您的demo.py文件已损坏 - 请确保开括号字符确实存在。从project page对安装程序进行Grub以确保。

您可以访问文件的current SVN version(选择HEAD修订版的 download )。

答案 3 :(得分:0)

如果我将代码复制并粘贴到Emacs中,我会得到:

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)

如果我删除三行开头的空白,它对我有效。

答案 4 :(得分:-1)

我一直在使用pylab。 Pylab homepage

来自debian repos:

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