Class: Numo::Gnuplot::PlotData

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

Direct Known Subclasses

ImageData, SPlotRecord

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data) ⇒ PlotData

Returns a new instance of PlotData.


815
816
817
818
819
820
821
822
# File 'lib/numo/gnuplot.rb', line 815

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


869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# File 'lib/numo/gnuplot.rb', line 869

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


824
825
826
827
828
829
830
# File 'lib/numo/gnuplot.rb', line 824

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

#data_formatObject


811
812
813
# File 'lib/numo/gnuplot.rb', line 811

def data_format
  @data_format || DATA_FORMAT
end

#data_strObject


832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/numo/gnuplot.rb', line 832

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


849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/numo/gnuplot.rb', line 849

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