require 'daru/view'
Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions. Install the mechanize gem version ~>2.7.5 for using mechanize functions.
true
dv = Daru::Vector.new [:a, :a, :a, :b, :b, :c], type: :category
Daru::Vector(6):category | |
---|---|
0 | a |
1 | a |
2 | a |
3 | b |
4 | b |
5 | c |
bar_graph1 = Daru::View::Plot.new(dv, type: :bar)
#<Daru::View::Plot:0x007fb2cd429d20 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fb2cd429ca8 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fb2cd423f88 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"b95cc6df-7f7f-40a4-9059-d751b75baa84"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{}}>>
bar_graph1.chart.class
Nyaplot::Plot
bar_graph1.show_in_iruby
dv = Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5, type: :category, categories: ['I', 'II', 'III']
dv.type
:category
bar_graph2 = Daru::View::Plot.new(dv, type: :bar)
#<Daru::View::Plot:0x007fb2cc2d1ac8 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fb2cc2d1a78 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fb2cc2d0858 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"e440efae-8482-48c6-97dc-06c3b629a358"}, @xrange=["I", "II", "III"], @yrange=[0, 10]>], :options=>{}}>>
bar_graph2.class
Daru::View::Plot
bar_graph2.chart.class
Nyaplot::Plot
# Nyaplot methods will work.Since #chart is Nyaplot::Plot class object
bar_graph2.chart.x_label 'Cat'
bar_graph2.chart.y_label 'Frequency'
bar_graph2.chart # or bar_graph2.show_in_iruby
bar_graph2_per = Daru::View::Plot.new(dv, type: :bar, method: :percentage)
#<Daru::View::Plot:0x007fb2cd384938 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fb2cd384910 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fb2cc9737f0 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"ccbe84da-ad90-4bee-b7d3-285b37d7f648"}, @xrange=["I", "II", "III"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100]}}>>
bar_graph2_per.chart.x_label 'Categories'
bar_graph2_per.chart.y_label 'Percentage (%)'
bar_graph2_per.show_in_iruby
bar_graph2_frac = Daru::View::Plot.new(dv, type: :bar, method: :fraction)
#<Daru::View::Plot:0x007fb2cd1f2598 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fb2cd1f24f8 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fb2cd1f0630 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"8df6373e-fdd1-4232-84f5-2aad579fc334"}, @xrange=["I", "II", "III"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1]}}>>
# bar_graph2_frac.chart.x_label 'Categories'
bar_graph2_frac.chart.y_label 'Fraction'
bar_graph2_frac.show_in_iruby
df = Daru::DataFrame.new({
a: [1, 2, 4, -2, 5, 23, 0],
b: [3, 1, 3, -6, 2, 1, 0],
c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']
})
df.to_category :c
df[:c].type
:category
bar_graph3 = Daru::View::Plot.new(df, type: :bar, x: :c)
#<Daru::View::Plot:0x007fb2cc9387b8 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fb2cc938650 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fb2cc923b10 @properties={:type=>:bar, :options=>{:value=>:c}, :data=>"3be85fac-17f7-48c4-aff7-13f61385b9b4"}, @xrange=["I", "II", "III"], @yrange=[0, 7]>], :options=>{}}>>
bar_graph3.show_in_iruby
df = Daru::DataFrame.new({
a: [1, 2, 4, -2, 5, 23, 0],
b: [3, 1, 3, -6, 2, 1, 0],
c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']
})
df.to_category :c
df[:c].type
:category
scatter_1 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color})
#<Daru::View::Plot:0x007fb2cd8499c8 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fb2cd849978 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fb2cc8792f0 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"I", :color=>"rgb(166,206,227)", :tooltip_contents=>["I", "I", "I"]}, :data=>"bc150eb5-c9ae-4b37-ad52-6d633415a76d"}, @xrange=[1, 5], @yrange=[2, 3]>, #<Nyaplot::Diagram:0x007fb2cc842b38 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"II", :color=>"rgb(31,120,180)", :tooltip_contents=>["II", "II"]}, :data=>"3f002967-c3d7-4f14-9aa7-70b884f0a65e"}, @xrange=[0, 2], @yrange=[0, 1]>, #<Nyaplot::Diagram:0x007fb2cd0d2488 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"III", :color=>"rgb(178,223,138)", :tooltip_contents=>["III", "III"]}, :data=>"44bf9e88-ac4d-48fd-8d8c-1dde044153d6"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true}}>>
scatter_1.show_in_iruby
scatter_1.chart.xrange [-10, 10]
scatter_1.chart.yrange [-10, 10]
scatter_2 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :shape})
scatter_2.show_in_iruby
scatter_2.chart.xrange [-10, 10]
scatter_2.chart.yrange [-10, 10]
scatter_3 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color, color: [:red, :blue, :green]})
scatter_3.show_in_iruby
scatter_2.chart.xrange [-10, 10]
scatter_2.chart.yrange [-10, 10]
scatter_4 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :size, size: [300, 600, 900]})
scatter_4.show_in_iruby
scatter_4.chart.xrange [-10, 10]
scatter_4.chart.yrange [-10, 10]
It works similar to Scatter plot above and all options are same except that there's no categorization by size but instead there is categorization by stroke_width in line plots.
df = Daru::DataFrame.new({
a: [1, 2, 3, 4, 5, 6, 7, 8, 9],
b: [2, 4, 6, 1, 3, 5, 6, 4, 3],
c: ['I']*3 + ['II']*3 + ['III']*3
})
df.to_category :c
df[:c].type
:category
line_1 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :color})
line_1.show_in_iruby
line_2 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :stroke_width})
line_2.show_in_iruby
line_2.chart.xrange [-10, 10]
line_2.chart.yrange [-10, 10]