【问题标题】:Using gnuplot to generate a set of numbered images使用 gnuplot 生成一组编号图像
【发布时间】:2013-05-03 02:09:18
【问题描述】:

我是 Python 的新手,我想问你,我怎样才能获得由 Gnuplot.py 制作的名称为变量的图像(很多图像)?我有这个功能,它创建单个图像:

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

我仍然无法摆脱错误“未定义的变量:文件名”。

谢谢, 马吉兹利克

【问题讨论】:

    标签: python variables filenames gnuplot


    【解决方案1】:

    现在,你的 python 脚本正在通过

    set out filename
    

    到 gnuplot。 'filename' 是命令字符串的一部分;您在脚本中设置的变量filename 没有被传递给gnuplot。您可以尝试更换

    g('set out filename')
    

    与

    g('set out "'+filename+'"')
    

    【讨论】:

    • 仍然是同样的错误,未定义的变量:out203... python 说“out203”而不是“out203.postscript”这一事实是否会成为问题的关键?是否有可能,该文件的名称被解释为另一个变量?顺便提一句。我将带有变量的行编辑为:filename = "out" + str(i) + ".postscript"
    • 我的错误,文件名需要用引号引起来。请参阅我的编辑。 Gnuplot 将正确创建一个 postscript 文件 out203,但您可能需要添加扩展名,以便其他程序知道它是一个 postscript 文件。
    【解决方案2】:

    也许你可以使用hardcopy 方法?

    文档

    hardcopy (
            self,
            filename=None,
            terminal='postscript',
            **keyw,
            )
    

    创建当前绘图的硬拷贝。

    为默认打印机创建当前绘图的 postscript 硬拷贝 (如果已配置)或指定的文件名。

    请注意,gnuplot 会记住跨终端的 postscript 子选项 变化。因此,例如,如果您为一份硬拷贝设置 color=1 那么除非您明确选择,否则下一个硬拷贝也将是彩色的 颜色=0。或者,您可以强制所有选项为其默认值 通过设置模式=默认。我认为这是 gnuplot 中的一个错误。

    示例

    见example call:

    g.hardcopy('gp_test.ps', enhanced=1, color=1)
    

    【讨论】:

      【解决方案3】:

      Gnuplot 需要set output "filename" 形式的行。请注意,文件名必须是字符串。因此,对于您的示例,它将类似于:

      g('set out "%s"'%filename)
      

      或使用更新的字符串格式:

      g('set out "{0}"'.format(filename))
      

      还有其他一些可以做得更好的事情。一般来说:

      i = 0
      for x in whatever:
          ...
          i = i + 1
      

      最好写成:

      for i,x in enumerate(whatever):
          ...
      

      另外,再次使用字符串格式:

      str(i) + " " + row[22:]
      

      可以转化为:

      '%d %s'%(i,row[22:])
      

      或:

      '{0} {1}'.format(i,row[22:])
      

      这些都是小事,在这个脚本中可能不会有太大的不同,但记住它们会很好,以备将来使用。

      【讨论】:

      • 谢谢你,我很高兴你的每一个建议。 :-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2012-04-14
      相关资源
      最近更新 更多