Note : Examples are taken from daru : http://nbviewer.jupyter.org/github/SciRuby/sciruby-notebooks/blob/master/Data%20Analysis/Plotting/Visualization.ipynb
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
Daru::View.plotting_library = :nyaplot
:nyaplot
dv = Daru::Vector.new [:a, :a, :a, :b, :b, :c], type: :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:0x00000003b87dd0 @data=#<Daru::Vector(6):category> 0 a 1 a 2 a 3 b 4 b 5 c, @options={:type=>:bar, :method=>:count}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x00000003b876f0 @properties={:diagrams=>[#<Nyaplot::Diagram:0x00000003b85f08 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"d2703cc9-f28f-4c6a-adcf-3be6187d1969"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{}}>>
bar_graph1.chart.class
Nyaplot::Plot
bar_graph1.show_in_iruby
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
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:0x00000003a3ca20 @data=#<Daru::Vector(20):category> 0 III 1 III 2 III 3 III 4 III 5 III 6 III 7 III 8 III 9 III 10 II 11 II 12 II 13 II 14 II ... ..., @options={:type=>:bar, :method=>:count}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x00000003a3c930 @properties={:diagrams=>[#<Nyaplot::Diagram:0x00000003a33600 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"5c7ce333-5a85-4667-99dc-70f28f8c1f03"}, @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:0x00000003192668 @data=#<Daru::Vector(20):category> 0 III 1 III 2 III 3 III 4 III 5 III 6 III 7 III 8 III 9 III 10 II 11 II 12 II 13 II 14 II ... ..., @options={:type=>:bar, :method=>:percentage}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x00000003192500 @properties={:diagrams=>[#<Nyaplot::Diagram:0x00000003190890 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"4cfa1107-b2e6-49bb-9c04-375158cfe800"}, @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
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
bar_graph2_frac = Daru::View::Plot.new(dv, type: :bar, method: :fraction)
#<Daru::View::Plot:0x00000002edcbd0 @data=#<Daru::Vector(20):category> 0 III 1 III 2 III 3 III 4 III 5 III 6 III 7 III 8 III 9 III 10 II 11 II 12 II 13 II 14 II ... ..., @options={:type=>:bar, :method=>:fraction}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x00000002edcb58 @properties={:diagrams=>[#<Nyaplot::Diagram:0x00000002ed19d8 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"9d2ff911-93f1-4a5e-b266-e403eafe49b5"}, @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
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
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:0x0000000274ff48 @data=#<Daru::DataFrame(7x3)> a b c 0 1 3 I 1 2 1 II 2 4 3 I 3 -2 -6 III 4 5 2 I 5 23 1 III 6 0 0 II, @options={:type=>:bar, :x=>:c}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x0000000274e9e0 @properties={:diagrams=>[#<Nyaplot::Diagram:0x0000000266e4d0 @properties={:type=>:bar, :options=>{:value=>:c}, :data=>"71214b09-e3c4-48cf-95d1-5000d0600ded"}, @xrange=["I", "II", "III"], @yrange=[0, 7]>], :options=>{}}>>
bar_graph3.show_in_iruby
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
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:0x00000003c12f70 @data=#<Daru::DataFrame(7x3)> a b c 0 1 3 I 1 2 1 II 2 4 3 I 3 -2 -6 III 4 5 2 I 5 23 1 III 6 0 0 II, @options={:type=>:scatter, :x=>:a, :y=>:b, :categorized=>{:by=>:c, :method=>:color}}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#<Nyaplot::Plot:0x00000003c12e58 @properties={:diagrams=>[#<Nyaplot::Diagram:0x00000003c07648 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"I", :color=>"rgb(141,211,199)", :tooltip_contents=>["I", "I", "I"]}, :data=>"00976c4d-e2ef-4329-8154-3c8442b6494c"}, @xrange=[1, 5], @yrange=[2, 3]>, #<Nyaplot::Diagram:0x00000003bfd350 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"II", :color=>"rgb(255,255,179)", :tooltip_contents=>["II", "II"]}, :data=>"6f96eb03-6646-4b90-9396-375d8821d5cb"}, @xrange=[0, 2], @yrange=[0, 1]>, #<Nyaplot::Diagram:0x00000003befd40 @properties={:type=>:scatter, :options=>{:x=>:a, :y=>:b, :title=>"III", :color=>"rgb(190,186,218)", :tooltip_contents=>["III", "III"]}, :data=>"36e2cd12-ae08-4302-8438-eeb6fd1b29f1"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true}}>>
scatter_1.show_in_iruby
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
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
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
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
#<CZTop::Socket::PUB:0x35d48a0 last_endpoint="tcp://127.0.0.1:49287">
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]