将ruby Gnuplot写入文件

将ruby Gnuplot写入文件,ruby,gnuplot,Ruby,Gnuplot,我在将Gnuplot保存到文件时遇到问题。这是我的代码: def plot(a, b, name) o = Gnuplot.open do |gp| Gnuplot::Plot.new( gp ) do |plot| plot.title "Wykres funkcji" plot.autoscale #plot.output name+".svg" plot.term "jpeg" plot.ylabel "x"

我在将Gnuplot保存到文件时遇到问题。这是我的代码:

def plot(a, b, name)

  o = Gnuplot.open do |gp|
    Gnuplot::Plot.new( gp ) do |plot|
      plot.title "Wykres funkcji"
      plot.autoscale
      #plot.output name+".svg"
      plot.term "jpeg"
      plot.ylabel "x"
      plot.xlabel "y"
      plot.grid
      x = (a..b) .collect { |v|v.to_f }
      y = x.collect { |v| value(v)}
      plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
        ds.with = "lines"
      end
    end
    File.open("test.jpeg", "w"){|to_file| Marshal.dump(o, to_file)}
  end
end
我可以通过将文件作为函数的参数来实现这一点吗?

在项目中,有一个关于如何将图像输出到文件的示例,如中所述:

乍一看,您最初的问题似乎只是由设置plot.term而不是plot.terminal引起的

此外,我不明白为什么需要使用Marshal.dump和File.open—您可以使用plot.output写入给定的文件名,如上面的示例所示。在这里使用Marshal.dump甚至不起作用,因为您正在转储整个Gnuplot对象,而不仅仅是一个JPEG文件


如果您真的想实现您的方法来获取文件对象,而不只是使用文件名字符串,那么您可以考虑告诉GNUTRAP写入A,然后将TEMPFILE复制到您自己的文件对象中?

我知道我可以通过使用PLUT输出来编写,但是我可以在不使用它的情况下另外做吗?为什么?你们能用一个临时文件吗?当有人说不要使用输出时,我有任务要做。我认为使用Tempfile不太好。我还有其他选择吗?也许使用rmagici不知道如何使用它?你的任务是不使用输出,但你需要生成一个输出文件?我不明白。如果给您一个file对象,而不仅仅是一个文件名,那么您可以简单地使用:是的,我同意Tempfile不是一个好的解决方案-这就是为什么我说只使用输出!
 Error interpreting JPEG image file (Not a JPEG file: starts with 0x04 0x08)
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require "gnuplot"

# See sin_wave.rb first
Gnuplot.open do |gp|
  Gnuplot::Plot.new( gp ) do |plot|

    # The following lines allow outputting the graph to an image file. 
    # The first set the kind of image that you want, while the second
    # redirects the output to a given file. 
    #
    # Typical terminals: gif, png, postscript, latex, texdraw
    #
    # See http://mibai.tec.u-ryukyu.ac.jp/~oshiro/Doc/gnuplot_primer/gptermcmp.html
    # for a list of recognized terminals.
    #
    plot.terminal "gif"
    plot.output File.expand_path("../sin_wave.gif", __FILE__)

    # see sin_wave.rb
    plot.xrange "[-10:10]"
    plot.title  "Sin Wave Example"
    plot.ylabel "sin(x)"
    plot.xlabel "x"

    plot.data << Gnuplot::DataSet.new( "sin(x)" ) do |ds|
      ds.with = "lines"
      ds.linewidth = 4
    end

  end
end
puts 'created sin_wave.gif'