Module: Plotrb::Simple

Defined in:
lib/plotrb/simple.rb

Constant Summary collapse

SCATTER_DATA_NAME =
'scatter'
SCATTER_X_SCALE_NAME =
'scatter_x'
SCATTER_Y_SCALE_NAME =
'scatter_y'

Class Method Summary collapse

Class Method Details

.scatter(x, y, kwargs = {}) ⇒ Plotrb::Visualization

Generate a simple 2d scatter plot.

method signature for ruby 2.0 kwargs: def scatter(x, y, symbol: ‘circle’, color: ‘blue’, markersize: 20,

width: 640, height: 480, domain: nil, range: nil)

Parameters:

  • x (NMatrix, Array)

    the x datapoints; if a single row, will be used for all y dataseries, if multiple rows, each row of x will be used for the corresponding row of y

  • y (NMatrix, Array)

    the y datapoints. Can be a single dimensional array, or 2D with multiple series in rows; column dimension should match the number of elements in x.

  • symbol (String, Array<String>)

    the type of symbol to be used to plot the points. Can be any symbol Vega understands: circle, square, cross, diamond, triangle-up, triangle-down. If a single String is provided, this will be used for all the points. If an Array of Strings is provided, each symbol in the array will be used for the corresponding data series (row) in y. Default: ‘circle’

  • color (String, Array<String>)

    the color to be used to plot the points. If a single String is provided, this will be used for all the points. If an Array of Strings is provided, each color in the array will be used for the corresponding data series (row) in y. Default: ‘blue’

  • markersize (Numeric)

    the size of the marker in pixels. Default: 20

  • width (Numeric)

    the visualization width in pixels. Default: 640

  • height (Numeric)

    the visualization height in pixels. Default: 480

  • domain (Array, String)

    the domain for the plot (limits on the x-axis). This can be a 2-element array of bounds or any other object that Plotrb::Scale::from understands. Default: scale to x data

  • range (Array, String)

    the range for the plot (limits on the y-axis). This can be a 2-element array of bounds or any other object that Plotrb::Scale::from understands. Default: scale to first row of y.

Returns:

  • (Plotrb::Visualization)

    A visualization object. (This can be written to a json string for Vega with #generate_spec.)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/plotrb/simple.rb', line 97

def self.scatter(x, y, kwargs={})
  kwargs = {symbol: 'circle', color: 'blue', markersize: 20, width: 640,
            height: 480, domain: nil, range: nil}.merge(kwargs)
  symbol = kwargs[:symbol]
  color = kwargs[:color]
  markersize = kwargs[:markersize]
  width = kwargs[:width]
  height = kwargs[:height]
  domain = kwargs[:domain]
  range = kwargs[:range]
  
  datapoints = []
  n_sets = 1
  x_n_sets = 1
  x_size = x.size

  if x.respond_to?(:shape) and x.shape.length > 1 then  # x is 2D NMatrix
    x_n_sets = x.shape[0]
    x_size = x.shape[1]
  elsif x.instance_of? Array and x[0].instance_of? Array then # x is nested Array
    x_n_sets = x.size
    x_size = x[0].size
  end

  if y.respond_to?(:shape) and y.shape.length > 1 then # y is 2D NMatrix
    n_sets = y.shape[0]
  elsif y.instance_of? Array and y[0].instance_of? Array then # y is nested array
    n_sets = y.size
  end

  x_size.times do |i|
    dp = {}
    n_sets.times do |j|

      xj = j.modulo(x_n_sets)
      if x.respond_to?(:shape) and x.shape.length > 1 then
        dp["x#{xj}".to_sym] = x[xj, i]
      elsif x.instance_of? Array and x[0].instance_of? Array then
        dp["x#{xj}".to_sym] = x[xj][i]
      else
        dp["x#{xj}".to_sym] = x[i]
      end

      indices = [i]
      if y.respond_to?(:shape) and y.shape.length > 1 then
        indices = [j,i]
      end
      if y.instance_of? Array and y[0].instance_of? Array then
        dp["y#{j}".to_sym] = y[j][*indices]
      else
        dp["y#{j}".to_sym] = y[*indices]
      end
    end

    datapoints << dp
  end

  Plotrb::Kernel.data.delete_if { |d| d.name == scatter_data_name }
  dataset= Plotrb::Data.new.name(scatter_data_name)
  dataset.values(datapoints)

  domain_in = "#{scatter_data_name}.x0"
  if domain then
    domain_in = domain
  end
  range_in = "#{scatter_data_name}.y0"
  if range then
    range_in = range
  end

  Plotrb::Kernel.scales.delete_if { |d| d.name == scatter_x_scale_name or d.name == scatter_y_scale_name }

  xs = linear_scale.name(scatter_x_scale_name).from(domain_in).to_width
  ys = linear_scale.name(scatter_y_scale_name).from(range_in).to_height

  marks = []
  n_sets.times do |j|
    marks << symbol_mark.from(dataset) do
      c_j = color.instance_of?(Array) ? color[j] : color
      s_j = symbol.instance_of?(Array) ? symbol[j] : symbol
      x_j = j.modulo(x_n_sets)
      enter do
        x_start { scale(xs).from("x#{x_j}") }
        y_start { scale(ys).from("y#{j}") }
        size markersize
        shape s_j
        fill c_j
      end
    end
  end

  visualization.width(width).height(height) do
    data dataset
    scales xs, ys
    marks marks
    axes x_axis.scale(xs), y_axis.scale(ys)
  end
end

.scatter_data_nameObject

Data name used by scatter plot



43
44
45
# File 'lib/plotrb/simple.rb', line 43

def self.scatter_data_name
  SCATTER_DATA_NAME
end

.scatter_x_scale_nameObject

Scale name used by scatter plot for x axis



50
51
52
# File 'lib/plotrb/simple.rb', line 50

def self.scatter_x_scale_name
  SCATTER_X_SCALE_NAME
end

.scatter_y_scale_nameObject

Scale name used by scatter plot for y axis



57
58
59
# File 'lib/plotrb/simple.rb', line 57

def self.scatter_y_scale_name
  SCATTER_Y_SCALE_NAME
end