1

我有一个 csv 文件“harvest.csv”,其中一列包含日期。这是我来到的(plot.rb):

require 'csv'
require 'gnuplot'

days = Array.new
mg = Array.new

csv = CSV.open("../data/harvest.csv", headers: :first_row, converters: :numeric)
csv.each do |row|
  days << row[1]
  mg << row[3]
end


dates = []
days.each {|n| dates << Date.strptime(n,"%Y-%m-%d")}

Gnuplot.open do |gp|
  Gnuplot::Plot.new( gp ) do |plot|
    plot.timefmt "'%Y%m%d'"
    plot.title  "Best Harvest Day"
    plot.xlabel "Time"
    **plot.xrange "[('2013-04-01'):('2013-06-01')]"**
    plot.ylabel "Harvested"


    plot.data << Gnuplot::DataSet.new( [dates,mg] ) do |ds|
      ds.with = "linespoints"
      ds.title = "Pollen harvested"
    end
  end
end

当我运行 plot.rb 时出现错误:

 line 735: Can't plot with an empty x range!

我应该将 [日期] 转换为其他内容吗?

4

1 回答 1

1

您设置的格式plot.timefmt必须与您在范围内使用的格式相匹配。现在-失踪了。此外,您需要设置 xdatatimex 轴上的数据类型设置为 time

Gnuplot::Plot.new(gp) do |plot|
  plot.timefmt "'%Y-%m-%d'"
  plot.title  "Best Harvest Day"
  plot.xlabel "Time"
  plot.xdata "time"
  plot.xrange '["2013-04-01":"2013-06-01"]'
  plot.ylabel "Harvested"

  plot.data << Gnuplot::DataSet.new([dates, mg]) do |ds|
    ds.with = "linespoints"
    ds.title = "Pollen harvested"
    ds.using = "1:2"
  end
end
于 2015-10-30T23:14:29.280 回答