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:0x007fedb3aab420 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fedb3aab3a8 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fedb3aa86f8 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"f71ed3e1-f71c-4d31-8b9c-87afa980bd26"}, @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:0x007fedb2a71938 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fedb2a71910 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fedb2a703f8 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"0b4f6d25-5c36-4692-be4b-9e990e52524c"}, @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:0x007fedb3aba830 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fedb3aba808 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fedb3ab92a0 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"2d91e56c-fdbc-4d3c-acc1-25d9ac498ff1"}, @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:0x007fedb3a210e0 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fedb3a210b8 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fedb3a1bc58 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"de67e9c8-89a1-4ebc-9168-78c1a254f5d3"}, @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:0x007fedb2a505d0 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fedb2a504b8 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fedb2a28968 @properties={:type=>:bar, :options=>{:value=>:c}, :data=>"c1c752e6-e52f-4acf-b295-752e8427507d"}, @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:0x007fedb38eb888 @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x007fedb38eb838 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007fedb38d8760 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"I", :color=>"rgb(179,226,205)", :tooltip_contents=>["I", "I", "I"]}, :data=>"98c86027-999b-4c4c-bbfc-c618a33552ce"}, @xrange=[1, 5], @yrange=[2, 3]>, #<Nyaplot::Diagram:0x007fedb226bb80 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"II", :color=>"rgb(253,205,172)", :tooltip_contents=>["II", "II"]}, :data=>"fb73976e-b3ed-4c03-a61d-83b77bcab567"}, @xrange=[0, 2], @yrange=[0, 1]>, #<Nyaplot::Diagram:0x007fedb2260de8 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"III", :color=>"rgb(203,213,232)", :tooltip_contents=>["III", "III"]}, :data=>"c1bf3eb7-5da0-484b-8fdc-2630ae05d027"}, @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]