SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.03k stars 139 forks source link

Nyaplot uses index as default axis for vector but not for dataframe #402

Closed baarkerlounger closed 5 years ago

baarkerlounger commented 6 years ago
df = Daru::DataFrame.new({y1: [11, 15, 8], y2: [9, 10, 3]}, 
     index: ["a", "b", "c"])
df.plot type: :bar, y: :y1 do |plot, diagram|
  plot.y_label "Frequency"
  plot.x_label "Category"
  plot.yrange [0,20]
end

Throws NoMethodError: undefined methodto_sym' for nil:NilClass`

I would expect it to behave like Daru::Vector and take the index as default axis.

dv = df[:y1]
dv.plot type: :bar, y: :y1 do |plot, diagram|
  plot.y_label "Frequency"
  plot.x_label "Category"
  plot.yrange [0,20]
end

image

matugm commented 6 years ago

Full stacktrace for reference:

/home/jesus/.gem/ruby/2.4.1/gems/nyaplot-0.1.6/lib/nyaplot/data.rb:105:in `column': undefined method `to_sym' for nil:NilClass (NoMethodError)
    from /home/jesus/.gem/ruby/2.4.1/gems/nyaplot-0.1.6/lib/nyaplot/diagram.rb:69:in `process_data'
    from /home/jesus/.gem/ruby/2.4.1/gems/nyaplot-0.1.6/lib/nyaplot/diagram.rb:16:in `initialize'
    from /home/jesus/.gem/ruby/2.4.1/gems/nyaplot-0.1.6/lib/nyaplot/plot.rb:75:in `new'
    from /home/jesus/.gem/ruby/2.4.1/gems/nyaplot-0.1.6/lib/nyaplot/plot.rb:75:in `add_with_df'
    from /home/jesus/.gem/ruby/2.4.1/gems/daru-0.1.6/lib/daru/plotting/nyaplot/dataframe.rb:174:in `block in add_multiple_diagrams'
    from /home/jesus/.gem/ruby/2.4.1/gems/daru-0.1.6/lib/daru/plotting/nyaplot/dataframe.rb:173:in `map'
    from /home/jesus/.gem/ruby/2.4.1/gems/daru-0.1.6/lib/daru/plotting/nyaplot/dataframe.rb:173:in `add_multiple_diagrams'
    from /home/jesus/.gem/ruby/2.4.1/gems/daru-0.1.6/lib/daru/plotting/nyaplot/dataframe.rb:143:in `plot_regular_diagrams'
    from /home/jesus/.gem/ruby/2.4.1/gems/daru-0.1.6/lib/daru/plotting/nyaplot/dataframe.rb:47:in `plot_without_category'
    from /home/jesus/.gem/ruby/2.4.1/gems/daru-0.1.6/lib/daru/plotting/nyaplot/dataframe.rb:32:in `plot'
    from test.rb:7:in `<main>'

The nil value is coming from opts[:x] being nil since it's not set when calling the plot method.

ChepChaf commented 5 years ago

Shouldn't this be closed?