-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlineplot.cr
More file actions
36 lines (30 loc) · 1.04 KB
/
lineplot.cr
File metadata and controls
36 lines (30 loc) · 1.04 KB
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
require "../src/unicode_plot"
include UnicodePlot
# Basic line plot
p = lineplot([1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 4.0, 9.0, 16.0, 25.0],
title: "Quadratic growth", xlabel: "input", ylabel: "response")
puts p
puts
# Sine wave
x = (0..62).map { |i| i * Math::PI / 31.0 }
y = x.map { |v| Math.sin(v) }
p = lineplot(x, y, title: "Periodic oscillation", xlabel: "phase", ylabel: "amplitude", color: :blue)
puts p
puts
# Multiple series with lineplot!
x = (1..20).map(&.to_f)
p = lineplot(x, x.map { |v| Math.sqrt(v) }, name: "root", color: :green,
title: "Transform comparison", xlabel: "input")
lineplot!(p, x, x.map { |v| Math.log(v) }, name: "log", color: :red)
puts p
puts
# Plotting a function
p = lineplot(-3.0, 3.0, ->(t : Float64) { Math.exp(-t * t / 2.0) / Math.sqrt(2 * Math::PI) },
title: "Bell-shaped kernel", xlabel: "input", ylabel: "density")
puts p
puts
# Log scale
x = (1..20).map(&.to_f)
p = lineplot(x, x.map { |v| v * v }, xscale: :log10, yscale: :log10,
title: "Scaled power relation", xlabel: "input", ylabel: "response")
puts p