Class: Numo::Gnuplot::PlotData

Inherits:
Object
  • Object
show all
Defined in:
lib/numo/gnuplot.rb

Direct Known Subclasses

SPlotArray, SPlotRecord

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data) ⇒ PlotData

Returns a new instance of PlotData.



752
753
754
755
756
757
758
759
# File 'lib/numo/gnuplot.rb', line 752

def initialize(*data)
  if data.empty?
    raise ArgumentError,"no data"
  end
  @data = data.map{|a| a.flatten}
  @n = @data.map{|a| a.size}.min
  @text = true
end

Class Method Details

.array_shape(a) ⇒ Object



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/numo/gnuplot.rb', line 806

def self.array_shape(a)
  if a.kind_of?(Array)
    is_2d = true
    is_1d = true
    size_min = nil
    a.each do |b|
      if b.kind_of?(Array)
        is_1d = false
        if b.any?{|c| c.kind_of?(Array)}
          is_2d = false
        elsif size_min.nil? || b.size < size_min
          size_min = b.size
        end
      else
        is_2d = false
      end
      break if !is_1d && !is_2d
    end
    if is_1d
      [a.size]
    elsif is_2d
      [a.size, size_min]
    else
      kernel_raise GnuplotError, "not suitable Array for data"
    end
  elsif a.respond_to?(:shape)
    a.shape
  else
    kernel_raise GnuplotError, "not suitable type for data"
  end
end

Instance Method Details

#cmd_strObject



761
762
763
764
765
766
767
# File 'lib/numo/gnuplot.rb', line 761

def cmd_str
  if @text
    "'-'"
  else
    "'-' binary record=#{@n} format='%float64'"
  end
end

#data_formatObject

:nodoc: all



748
749
750
# File 'lib/numo/gnuplot.rb', line 748

def data_format
  @data_format || DATA_FORMAT
end

#data_strObject



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/numo/gnuplot.rb', line 769

def data_str
  if @text
    s = ""
    @n.times{|i| s << line_str(i)+"\n"}
    s + "e"
  elsif defined? Numo::NArray
    m = @data.size
    x = Numo::DFloat.zeros(@n,m)
    m.times{|i| x[true,i] = @data[i][0...@n]}
    x.to_string
  else
    s = []
    @n.times{|i| s.concat @data.map{|a| a[i]}}
    s.pack("d*")
  end
end

#line_str(i) ⇒ Object



786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/numo/gnuplot.rb', line 786

def line_str(i)
  @data.map do |a|
    v = a[i]
    case v
    when Float,Rational
      s = data_format % v
    when Numeric
      s = v.to_s
    else
      s = v.to_s
      if /"/ =~ s
        kernel_raise GnuplotError,"should not include double quotation in data"
      else
        s = '"'+s+'"'
      end
    end
    s
  end.join(" ")
end