Skip to content
kojix2 edited this page Jun 26, 2020 · 15 revisions

Subplots

You can use the subplot method.

Example 1

require 'gr/plot'

x = [1,2,3,4,5,6,7,8,9,10]
y = x.shuffle

GR.barplot x, y, GR.subplot(2, 2, 1)
GR.stem    x, y, GR.subplot(2, 2, 2)
GR.step    x, y, GR.subplot(2, 2, 3)
GR.plot    x, y, GR.subplot(2, 2, 4)

subplot

Example 2

require 'gr/plot'

x = [1,2,3,4,5,6,7,8,9,10]
y = x.shuffle

GR.scatter x, y, GR.subplot(5, 5, [2, 4])
GR.plot    x, y, GR.subplot(5, 5, [6, 16])
GR.step    x, y, GR.subplot(5, 5, [10, 20])
GR.barplot x, y, GR.subplot(5, 5, [7, 19])
GR.stem    x, y, GR.subplot(5, 5, [22, 24], update: true)

subplot

subplot returns a simple hash like this.

GR.subplot(2,2,1)
=> {:subplot=>[0.0, 0.5, 0.5, 1.0], :clear=>true, :update=>false}

Why does the GR.rb subplot simply return a Hash, unlike GR.jl? This is because the position of the subplot is better when it can be fine-tuned by a human being. Tweak the position of the subplot by changing the numbers in the Hash.

The update and clear flag are determined by these simple rules.

  • if n==1 clear=>true else clear=>false
  • if n==end update=>true else update=>false

A customized example

require 'gr/plot'
DFloat = Numo::DFloat

ax = DFloat.new(20_000).rand_norm
ay = DFloat.new(20_000).rand_norm
bx = DFloat.new(20_000).rand_norm(3)
by = DFloat.new(20_000).rand_norm(3)
cx = DFloat.new(20_000).rand_norm(3)
cy = DFloat.new(20_000).rand_norm(-3)

x = DFloat.hstack [ax, bx, cx]
y = DFloat.hstack [ay, by, cy]

GR.shade x, y, subplot: [0.02, 0.2, 0.8, 0.98], clear: true, update: false
GR.histogram x, subplot: [0.135, 0.89, 0.8, 0.98], clear: false, nbins: 50, update: false
GR.histogram y, subplot: [0.02, 0.2, 0.0, 0.82], horizontal: true, xflip: true, nbins: 50, grid: false, clear: false, update: false
GR.hexbin x, y, subplot: [0.12, 1.0, 0.0, 0.82], clear: false, update: true
sleep 1.5

subplot