File: scatterplot.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# = Scatterplot
# Scatterplots can facilitate visual analysis along multiple dimensions, though care should be taken to avoid interference. In this example, we encode three dimensions: two are encoded using position, while the third is redundantly encoded as both area and color. 
$:.unshift(File.dirname(__FILE__)+"/../../lib")
require 'rubyvis'
data = pv.range(100).map {|x| 
  OpenStruct.new({x: x, y: rand(), z: 10**(2*rand)})
}


w = 400
h = 400

x = pv.Scale.linear(0, 99).range(0, w)
y = pv.Scale.linear(0, 1).range(0, h)

c = pv.Scale.log(1, 100).range("orange", "brown")

# The root panel.
vis = pv.Panel.new()
    .width(w)
    .height(h)
    .bottom(20)
    .left(20)
    .right(10)
    .top(5);

# Y-axis and ticks. 
vis.add(pv.Rule)
    .data(y.ticks())
    .bottom(y)
    .strokeStyle(lambda {|d| d!=0 ? "#eee" : "#000"})
  .anchor("left").add(pv.Label)
  .visible(lambda {|d|  d > 0 and d < 1})
  .text(y.tick_format)

# X-axis and ticks. 
vis.add(pv.Rule)
    .data(x.ticks())
    .left(x)
    .stroke_style(lambda {|d| d!=0 ? "#eee" : "#000"})
  .anchor("bottom").add(pv.Label)
  .visible(lambda {|d|  d > 0 and d < 100})
  .text(x.tick_format);

#/* The dot plot! */
vis.add(pv.Panel)
    .data(data)
  .add(pv.Dot)
  .left(lambda {|d| x.scale(d.x)})
  .bottom(lambda {|d| y.scale(d.y)})
  .stroke_style(lambda {|d| c.scale(d.z)})
  .fill_style(lambda {|d| c.scale(d.z).alpha(0.2)})
  .shape_size(lambda {|d| d.z})
  .title(lambda {|d| "%0.1f" % d.z})

vis.render()
puts vis.to_svg