Module: GR

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

Defined Under Namespace

Classes: Plot

Class Method Summary collapse

Class Method Details

._contour_Object



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

alias _contour_ contour

._contourf_Object



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

alias _contourf_ contourf

._hexbin_Object



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

alias _hexbin_ hexbin

._shade_Object



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

alias _shade_ shade

._surface_Object

(Plot) Draw a three-dimensional surface plot.



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

alias _surface_ surface

.barplot(*args) ⇒ Object

(Plot) Draw a bar plot.



1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
# File 'lib/gr/plot.rb', line 1745

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

  wc, hc = barcoordinates(heights, kv)
  horizontal = kv.delete(:horizontal)

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

.contour(*args) ⇒ Object

(Plot) Draw a contour plot.



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

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

.contourf(*args) ⇒ Object

(Plot) Draw a filled contour plot.



1685
1686
1687
# File 'lib/gr/plot.rb', line 1685

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

.heatmap(*args) ⇒ Object

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



1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
# File 'lib/gr/plot.rb', line 1594

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.



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

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

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

(Plot) Draw a histogram.



1773
1774
1775
1776
1777
1778
1779
# File 'lib/gr/plot.rb', line 1773

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



1797
1798
1799
1800
# File 'lib/gr/plot.rb', line 1797

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.



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

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

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

(Plot) Draw an isosurface.



1790
1791
1792
1793
1794
1795
# File 'lib/gr/plot.rb', line 1790

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.



1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
# File 'lib/gr/plot.rb', line 1615

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.



1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
# File 'lib/gr/plot.rb', line 1657

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.



1558
1559
1560
# File 'lib/gr/plot.rb', line 1558

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

.plot3(*args) ⇒ Object

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



1722
1723
1724
# File 'lib/gr/plot.rb', line 1722

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

.polar(*args) ⇒ Object

(Plot)



1712
1713
1714
# File 'lib/gr/plot.rb', line 1712

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

.polarheatmap(*args) ⇒ Object

(Plot) Draw a polarheatmap.



1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
# File 'lib/gr/plot.rb', line 1636

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)



1583
1584
1585
1586
1587
1588
1589
1590
# File 'lib/gr/plot.rb', line 1583

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.



1826
1827
1828
1829
1830
1831
1832
# File 'lib/gr/plot.rb', line 1826

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.



1573
1574
1575
# File 'lib/gr/plot.rb', line 1573

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

.scatter3(*args) ⇒ Object

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



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

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

.shade(*args) ⇒ Object

(Plot)



1733
1734
1735
# File 'lib/gr/plot.rb', line 1733

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

.stairs(*args) ⇒ Object

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



1568
1569
1570
# File 'lib/gr/plot.rb', line 1568

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

.stem(*args) ⇒ Object

(Plot) Draw a stem plot.



1578
1579
1580
# File 'lib/gr/plot.rb', line 1578

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

.step(*args) ⇒ Object

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



1563
1564
1565
# File 'lib/gr/plot.rb', line 1563

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

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

Set current subplot index.



1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
# File 'lib/gr/plot.rb', line 1803

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



1707
1708
1709
# File 'lib/gr/plot.rb', line 1707

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

.tricont(*args) ⇒ Object

(Plot) Draw a triangular contour plot.



1696
1697
1698
# File 'lib/gr/plot.rb', line 1696

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

.trisurf(*args) ⇒ Object

(Plot) Draw a triangular surface plot.



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

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

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

(Plot)



1738
1739
1740
1741
1742
# File 'lib/gr/plot.rb', line 1738

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.



1701
1702
1703
# File 'lib/gr/plot.rb', line 1701

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