require 'numplot'

# Draw a random sample from the normal distribution (sigma = 0.3)
# with Box-Muller's method. 
def rand_normal
  x = rand
  y = rand
  Math.sqrt(-2*Math.log(x))*Math.cos(2*Math::PI*y)*0.3
end

plotter = NumPlot::Plotter.new

x_sin_pi_x = (-1.0 .. 1.0).step(0.05).map{|x| [x, Math.sin(Math::PI*x)] }
plotter <<
  NumPlot::Dataset.rows(x_sin_pi_x, with: "points", title: "sin(x)")

xs = (0.0 .. 1.0).step(0.05)
squares = xs.map{|x| x**2 }
plotter <<
  NumPlot::Dataset.columns([xs, squares], with: "lines", title: "x^2")

normal_random_dist = Array.new(10000){ rand_normal }
plotter <<
  NumPlot::Dataset.histo(normal_random_dist,
                         binsize: 0.07, histogram_style: :density,
                         title: "a normal distribution")

plotter.xrange(-1.0 .. 1.0)
plotter.xlabel("x", font: NumPlot::Font["ariel", 12])
plotter.ylabel("y", font: NumPlot::Font["ariel"])

plotter.set("key", "below")

plotter.plot