Python 使用gnuplot生成一组编号的图像
我是Python的新手,我想问你,如何获得Gnuplot.py制作的名为variable的图像(很多图像)?我有这个函数,它创建单个图像:Python 使用gnuplot生成一组编号的图像,python,variables,filenames,gnuplot,Python,Variables,Filenames,Gnuplot,我是Python的新手,我想问你,如何获得Gnuplot.py制作的名为variable的图像(很多图像)?我有这个函数,它创建单个图像: def printimage(conf, i): filename = str(i) + "out.postscript" g = Gnuplot.Gnuplot() g.title('My Systems Plot') g.xlabel('Date') g.ylabel('Value') g('set term post
def printimage(conf, i):
filename = str(i) + "out.postscript"
g = Gnuplot.Gnuplot()
g.title('My Systems Plot')
g.xlabel('Date')
g.ylabel('Value')
g('set term postscript')
g('set out filename')
databuff = Gnuplot.File(conf, using='1:2',with_='line', title="test")
g.plot(databuff)
此函数用于for循环:
i = 0
for row in data:
config_soubor.write(str(i) + " " + row[22:])
printimage("config_soubor.conf", i)
i = i + 1
我仍然无法摆脱错误“未定义变量:文件名”
谢谢,
Majzlik也许你可以用这个方法
文档
创建当前打印的硬拷贝
将当前打印的postscript硬拷贝创建到默认打印机
(如果已配置)或指定的文件名
注意,gnuplot会记住终端上的postscript子选项
变化。因此,例如,如果将一个硬拷贝的颜色设置为1
除非您明确选择,否则下一个硬拷贝也将是彩色的
颜色=0。或者,您可以强制所有选项为其默认值
通过设置mode=default。我认为这是GnUpTr.< /P>中的一个错误。
例子
见:
现在,您的python脚本正在通过
set out filename
到gnuplot。“filename”是命令字符串的一部分;脚本中设置的变量filename
未传递给gnuplot。你可以试着换一个
g('set out filename')
与
Gnuplot需要一行格式为
设置输出“filename”
。请注意,文件名必须是字符串。所以,以你的例子来说,它应该是这样的:
g('set out "%s"'%filename)
或使用较新样式的字符串格式:
g('set out "{0}"'.format(filename))
str(i) + " " + row[22:]
还有一些事情可以做得更好。一般而言:
i = 0
for x in whatever:
...
i = i + 1
最好写为:
for i,x in enumerate(whatever):
...
同样,使用字符串格式:
g('set out "{0}"'.format(filename))
str(i) + " " + row[22:]
可转换为:
'%d %s'%(i,row[22:])
或:
这些都是很小的事情,在这个脚本中,这两种方式可能都不会有很大的区别,但是在将来记住它们会很好。仍然是相同的错误,未定义的变量:out203。。。python说的是“out203”而不是“out203.postscript”,这是问题的关键吗?是否可能,该文件的名称被解释为另一个变量?顺便说一句,我将带有变量的行编辑为:filename=“out”+str(I)+“.postscript”我的错误是,文件名需要加引号。请参阅我的编辑。Gnuplot将正确创建postscript文件
out203
,但您可能需要添加扩展名,以便其他程序知道它是postscript文件。谢谢,我很高兴听到您的建议。:-)
'%d %s'%(i,row[22:])
'{0} {1}'.format(i,row[22:])