require 'bundler/setup'
require 'daru'
true
geeknees
July 13, 2023
https://github.com/SciRuby/daru
To plot the graph, it is necessary to add gem ‘daru-view’.
It also requires adding gem ‘matrics’. After the PR below is merged, it will be resolved. https://github.com/SciRuby/daru/pull/541
df = Daru::DataFrame.new({
a: Array.new(100) {|i| i},
b: 100.times.map{rand}
})
df.plot type: :scatter, x: :a, y: :b
df = Daru::DataFrame.new({
:temperature => [30.4, 23.5, 44.5, 20.3, 34, 24, 31.45, 28.34, 37, 24],
:sales => [350, 150, 500, 200, 480, 250, 330, 400, 420, 560],
:city => ['Pune', 'Delhi']*5,
:staff => [15,20]*5
})
df
temperature | sales | city | staff | |
---|---|---|---|---|
0 | 30.4 | 350 | Pune | 15 |
1 | 23.5 | 150 | Delhi | 20 |
2 | 44.5 | 500 | Pune | 15 |
3 | 20.3 | 200 | Delhi | 20 |
4 | 34 | 480 | Pune | 15 |
5 | 24 | 250 | Delhi | 20 |
6 | 31.45 | 330 | Pune | 15 |
7 | 28.34 | 400 | Delhi | 20 |
8 | 37 | 420 | Pune | 15 |
9 | 24 | 560 | Delhi | 20 |
df.plot(type: :scatter, x: :temperature, y: :sales) do |plot, diagram|
plot.x_label "Temperature"
plot.y_label "Sales"
plot.yrange [100, 600]
plot.xrange [15, 50]
diagram.tooltip_contents([:city, :staff])
diagram.color(Nyaplot::Colors.qual) # set the color scheme for this diagram. See Nyaplot::Colors for more info.
diagram.fill_by(:city) # Change color of each point WRT to the city that it belongs to.
diagram.shape_by(:city) # Shape each point WRT to the city that it belongs to.
end