Module: GR

Defined in:
lib/gr/plot.rb,
lib/gr/plot/version.rb

Defined Under Namespace

Classes: Plot

Class Method Summary collapse

Class Method Details

._contour_Object



1664
# File 'lib/gr/plot.rb', line 1664

alias _contour_ contour

._contourf_Object



1670
# File 'lib/gr/plot.rb', line 1670

alias _contourf_ contourf

._hexbin_Object



1676
# File 'lib/gr/plot.rb', line 1676

alias _hexbin_ hexbin

._shade_Object



1718
# File 'lib/gr/plot.rb', line 1718

alias _shade_ shade

._surface_Object

(Plot) Draw a three-dimensional surface plot.



1693
# File 'lib/gr/plot.rb', line 1693

alias _surface_ surface

.barplot(*args) ⇒ Object

(Plot) Draw a bar plot.



1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
# File 'lib/gr/plot.rb', line 1732

def barplot(*args)
  kv = args.last.is_a?(Hash) ? args.pop : {}
  if args.length == 2
    labels, heights = args
  elsif args.length == 1
    heights = args[0]
    labels = (1..heights.length).map(&:to_s)
  else
    raise ArgumentError
  end

  labels = labels.map(&:to_s)
  wc, hc = barcoordinates(heights, kv)
  horizontal = kv.delete(:horizontal)
  create_plot(:bar, labels, heights, kv) do |plt|
    if horizontal
      plt.args = [[hc, wc, nil, nil, '']]
      plt.kvs[:yticks] = [1, 1]
      plt.kvs[:yticklabels] = labels
    else
      plt.args = [[wc, hc, nil, nil, '']]
      plt.kvs[:xticks] = [1, 1]
      plt.kvs[:xticklabels] = labels
    end
  end
end

.contour(*args) ⇒ Object

(Plot) Draw a contour plot.



1666
1667
1668
# File 'lib/gr/plot.rb', line 1666

def contour(*args)
  create_plot(:contour, *format_xyzc(*args))
end

.contourf(*args) ⇒ Object

(Plot) Draw a filled contour plot.



1672
1673
1674
# File 'lib/gr/plot.rb', line 1672

def contourf(*args)
  create_plot(:contourf, *format_xyzc(*args))
end

.heatmap(*args) ⇒ Object

(Plot) Draw a heatmap. (Plot) Draw a heatmap.



1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
# File 'lib/gr/plot.rb', line 1581

def heatmap(*args)
  kv = args.last.is_a?(Hash) ? args.pop : {}
  if args.length == 1
    z = args[0]
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
    ysize, xsize = z.shape
    z = z.reshape(xsize, ysize)
    x = (1..xsize).to_a
    y = (1..ysize).to_a
  elsif args.length == 3
    x, y, z = args
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
  else
    raise ArgumentError
  end
  create_plot(:heatmap, kv) do |plt|
    plt.args = [[x, y, z, nil, '']]
  end
end

.hexbin(*args) ⇒ Object

(Plot) Draw a hexagon binning plot.



1678
1679
1680
# File 'lib/gr/plot.rb', line 1678

def hexbin(*args)
  create_plot(:hexbin, *args)
end

.histogram(series, kv = {}) ⇒ Object

(Plot) Draw a histogram.



1760
1761
1762
1763
1764
1765
1766
# File 'lib/gr/plot.rb', line 1760

def histogram(series, kv = {})
  create_plot(:hist, series, kv) do |plt|
    nbins = plt.kvs[:nbins] || 0
    x, y = hist(series, nbins)
    plt.args = [[x, y, nil, nil, '']]
  end
end

.hold(flag = true) ⇒ Object



1784
1785
1786
1787
# File 'lib/gr/plot.rb', line 1784

def hold(flag = true)
  plt = GR::Plot.last_plot
  plt.kvs.slice(:window, :scale, :xaxis, :yaxis, :zaxis).merge({ ax: flag, clear: !flag })
end

.imshow(img, kv = {}) ⇒ Object

(Plot) Draw an image.



1769
1770
1771
1772
1773
1774
# File 'lib/gr/plot.rb', line 1769

def imshow(img, kv = {})
  img = Numo::DFloat.cast(img) # Umm...
  create_plot(:imshow, img, kv) do |plt|
    plt.args = [[nil, nil, img, nil, '']]
  end
end

.isosurface(v, kv = {}) ⇒ Object

(Plot) Draw an isosurface.



1777
1778
1779
1780
1781
1782
# File 'lib/gr/plot.rb', line 1777

def isosurface(v, kv = {})
  v = Numo::DFloat.cast(v) # Umm...
  create_plot(:isosurface, v, kv) do |plt|
    plt.args = [[nil, nil, v, nil, '']]
  end
end

.nonuniformheatmap(*args) ⇒ Object

(Plot) Draw a nonuniformheatmap.



1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
# File 'lib/gr/plot.rb', line 1602

def nonuniformheatmap(*args)
  kv = args.last.is_a?(Hash) ? args.pop : {}
  if args.length == 1
    z = args[0]
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
    ysize, xsize = z.shape
    z = z.reshape(xsize, ysize)
    x = (1..xsize).to_a
    y = (1..ysize).to_a
  elsif args.length == 3
    x, y, z = args
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
  else
    raise ArgumentError
  end
  create_plot(:nonuniformheatmap, kv) do |plt|
    plt.args = [[x, y, z, nil, '']]
  end
end

.nonuniformpolarheatmap(*args) ⇒ Object

(Plot) Draw a nonuniformpolarheatmap.



1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
# File 'lib/gr/plot.rb', line 1644

def nonuniformpolarheatmap(*args)
  kv = args.last.is_a?(Hash) ? args.pop : {}
  if args.length == 1
    z = args[0]
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
    ysize, xsize = z.shape
    z = z.reshape(xsize, ysize)
    x = (1..xsize).to_a
    y = (1..ysize).to_a
  elsif args.length == 3
    x, y, z = args
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
  else
    raise ArgumentError
  end
  create_plot(:nonuniformpolarheatmap, kv) do |plt|
    plt.args = [[x, y, z, nil, '']]
  end
end

.plot(*args) ⇒ Object

(Plot) Draw one or more line plots.



1545
1546
1547
# File 'lib/gr/plot.rb', line 1545

def plot(*args)
  create_plot(:line, *args)
end

.plot3(*args) ⇒ Object

(Plot) Draw one or more three-dimensional line plots.



1709
1710
1711
# File 'lib/gr/plot.rb', line 1709

def plot3(*args)
  create_plot(:plot3, *args)
end

.polar(*args) ⇒ Object

(Plot)



1699
1700
1701
# File 'lib/gr/plot.rb', line 1699

def polar(*args)
  create_plot(:polar, *args)
end

.polarheatmap(*args) ⇒ Object

(Plot) Draw a polarheatmap.



1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
# File 'lib/gr/plot.rb', line 1623

def polarheatmap(*args)
  kv = args.last.is_a?(Hash) ? args.pop : {}
  if args.length == 1
    z = args[0]
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
    ysize, xsize = z.shape
    z = z.reshape(xsize, ysize)
    x = (1..xsize).to_a
    y = (1..ysize).to_a
  elsif args.length == 3
    x, y, z = args
    z = Numo::DFloat.cast(z) if z.is_a?(Array)
  else
    raise ArgumentError
  end
  create_plot(:polarheatmap, kv) do |plt|
    plt.args = [[x, y, z, nil, '']]
  end
end

.polarhistogram(x, kv = {}) ⇒ Object

(Plot)



1570
1571
1572
1573
1574
1575
1576
1577
# File 'lib/gr/plot.rb', line 1570

def polarhistogram(x, kv = {})
  plt = GR::Plot.new(x, kv)
  plt.kvs[:kind] = :polarhist
  nbins = plt.kvs[:nbins] || 0
  x, y = hist(x, nbins)
  plt.args = [[x, y, nil, nil, '']]
  plt.plot_data
end

.savefig(filename, kv = {}) ⇒ Object

(Plot) Save the current figure to a file.



1813
1814
1815
1816
1817
1818
1819
# File 'lib/gr/plot.rb', line 1813

def savefig(filename, kv = {})
  GR.beginprint(filename)
  plt = GR::Plot.last_plot
  plt.kvs.merge!(kv)
  plt.plot_data(false)
  GR.endprint
end

.scatter(*args) ⇒ Object

(Plot) Draw one or more scatter plots.



1560
1561
1562
# File 'lib/gr/plot.rb', line 1560

def scatter(*args)
  create_plot(:scatter, *args)
end

.scatter3(*args) ⇒ Object

(Plot) Draw one or more three-dimensional scatter plots.



1714
1715
1716
# File 'lib/gr/plot.rb', line 1714

def scatter3(*args)
  create_plot(:scatter3, *args)
end

.shade(*args) ⇒ Object

(Plot)



1720
1721
1722
# File 'lib/gr/plot.rb', line 1720

def shade(*args)
  create_plot(:shade, *args)
end

.stairs(*args) ⇒ Object

(Plot) Draw one or more step or staircase plots.



1555
1556
1557
# File 'lib/gr/plot.rb', line 1555

def stairs(*args)
  create_plot(:stairs, *args)
end

.stem(*args) ⇒ Object

(Plot) Draw a stem plot.



1565
1566
1567
# File 'lib/gr/plot.rb', line 1565

def stem(*args)
  create_plot(:stem, *args)
end

.step(*args) ⇒ Object

(Plot) Draw one or more step or staircase plots.



1550
1551
1552
# File 'lib/gr/plot.rb', line 1550

def step(*args)
  create_plot(:step, *args)
end

.subplot(nr, nc, p, kv = {}) ⇒ Object

Set current subplot index.



1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
# File 'lib/gr/plot.rb', line 1790

def subplot(nr, nc, p, kv = {})
  xmin = 1
  xmax = 0
  ymin = 1
  ymax = 0
  p = [p] if p.is_a? Integer
  p.each do |i|
    r = (nr - (i - 1) / nc).to_f
    c = ((i - 1) % nc + 1).to_f
    xmin = [xmin, (c - 1) / nc].min
    xmax = [xmax, c / nc].max
    ymin = [ymin, (r - 1) / nr].min
    ymax = [ymax, r / nr].max
  end
  {
    subplot: [xmin, xmax, ymin, ymax],
    # The policy of clearing when p[0]==1 is controversial
    clear: p[0] == 1,
    update: p[-1] == nr * nc
  }.merge kv
end

.surface(*args) ⇒ Object



1694
1695
1696
# File 'lib/gr/plot.rb', line 1694

def surface(*args)
  create_plot(:surface, *format_xyzc(*args))
end

.tricont(*args) ⇒ Object

(Plot) Draw a triangular contour plot.



1683
1684
1685
# File 'lib/gr/plot.rb', line 1683

def tricont(*args)
  create_plot(:tricont, *format_xyzc(*args))
end

.trisurf(*args) ⇒ Object

(Plot) Draw a triangular surface plot.



1704
1705
1706
# File 'lib/gr/plot.rb', line 1704

def trisurf(*args)
  create_plot(:trisurf, *format_xyzc(*args))
end

.volume(v, kv = {}) ⇒ Object

(Plot)



1725
1726
1727
1728
1729
# File 'lib/gr/plot.rb', line 1725

def volume(v, kv = {})
  create_plot(:volume, v, kv) do |plt|
    plt.args = [[nil, nil, v, nil, '']]
  end
end

.wireframe(*args) ⇒ Object

(Plot) Draw a three-dimensional wireframe plot.



1688
1689
1690
# File 'lib/gr/plot.rb', line 1688

def wireframe(*args)
  create_plot(:wireframe, *format_xyzc(*args))
end