forked from SciRuby/rubyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Scatter Plot
Alish Dipani edited this page Apr 9, 2019
·
2 revisions
A small example for creating scatter plot with Magick back-end.
require 'rmagick'
$canvas_height = 100
$canvas_width = 100
$horizontal_border = 10
$vertical_border = 10
$canvas = Magick::ImageList.new
$canvas.new_image($canvas_height+2*$vertical_border, $canvas_width+2*$horizontal_border, Magick::HatchFill.new('white', 'gray90'))
def draw_axes
axes = Magick::Draw.new
axes.polyline($horizontal_border,$canvas_height+$vertical_border, $canvas_width+$horizontal_border,$canvas_height+$vertical_border,
$horizontal_border,$canvas_height+$vertical_border, $horizontal_border,$vertical_border)
axes.draw($canvas)
end
def transform_x(x, max_x)
($canvas_width * x ) / max_x + $horizontal_border
end
def transform_y(y, max_y)
($canvas_height * (max_y - y)) / max_y + $vertical_border
end
def write(filename)
$canvas.write(filename)
end
def display
$canvas.display
end
def scatter(x,y,marker_size)
max_x = x.max
max_y = y.max
x.each_with_index do |ix, idx_x|
iy = y[idx_x]
ix = transform_x(ix,max_x)
iy = transform_y(iy,max_y)
marker = Magick::Draw.new
marker.circle(ix,iy, ix+marker_size,iy)
marker.draw($canvas)
end
end
def run()
x = [10,12,74,65]
y = [150,100,57,46]
draw_axes
scatter(x,y,2)
write("example.png")
end
run()Saving the figure for the scatter plot for the points (10,150),(12,100),(74,57) and (65,46) gives the output: