require '~/workspace/daru/lib/daru.rb'
true
dv = Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5, type: :category, categories: ['I', 'II', 'III']
dv.type
:category
dv.plot(type: :bar) do |p, d|
p.x_label 'Categories'
p.y_label 'Frequency'
end
dv.plot(type: :bar, method: :percentage) do |p, d|
p.x_label 'Categories'
p.y_label 'Percentage (%)'
end
dv.plot(type: :bar, method: :fraction) do |p, d|
p.x_label 'Categories'
p.y_label 'Fraction'
end
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
df.plot(type: :bar, x: :c)
Plots can be categorized by
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
Below are few examples
df.plot(type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color}) do |p, d|
p.xrange [-10, 10]
p.yrange [-10, 10]
end
df.plot(type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :shape}) do |p, d|
p.xrange [-10, 10]
p.yrange [-10, 10]
end
One can also specify custom colors, size and shape. For example:
df.plot(type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color, color: [:red, :blue, :green]}) do |p, d|
p.xrange [-10, 10]
p.yrange [-10, 10]
end
df.plot(type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :size, size: [300, 600, 900]}) do |p, d|
p.xrange [-10, 10]
p.yrange [-10, 10]
end
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
df.plot type: :line, x: :a, y: :b, categorized: {by: :c, method: :color} do end
df.plot type: :line, x: :a, y: :b, categorized: {by: :c, method: :stroke_width} do |p, d|
p.xrange [-10, 10]
p.yrange [-10, 10]
end