Skip to content

Save Plot to a file

kojix2 edited this page Jun 18, 2020 · 10 revisions

Save Plot object to a image file

If you are using GR::Plot, call savefig method.

require 'gr/plot'

x = [1, 2, 3, 4, 5]
y = [1, 8, 27, 64, 125]

GR.plot(x, y)

You can specify the format by file name or extension. png ps gif bmp jpg tiff pdf svg

GR.savefig('image.png')

You can add keyword values.

GR.savefig('image.png', title: 'PNG')

Native GR

If you are working with low-level API, use beginprint and endprint.

GR.beginprint("image.png")
# plot something nice...
GR.endprint

Note: The format of images and videos that can be generated by GR.rb depends on the library you link.

Save a video file

Use beginprint and endprint.

require 'gr/plot'

x = []; y = []; z = []

GR.beginprint("video.gif")
              
500.times do |i|
  z <<  i / 100.0
  x <<  i**2 * Math.sin(i * Math::PI / 10.0) / 100.0
  y <<  i**2 * Math.cos(i * Math::PI / 10.0) / 100.0

  GR.plot3(x, y, z, 'p', title: "i = #{i}")
  GR.updatews
  sleep 0.005
end

GR.endprint

video