Creating graphs in Ruby

I was trying to make some graphics with Ruby to test features of SciRuby. Well, I know Rubyvis, which was created by Claudio Bustos some time ago — it’s a gem to create SVG files.

A SVG (Scalable Vector Graphics) file is:

(…) an XML-based vector image format for two-dimensional graphics that has support for interactivity and animation. The SVG specification is an open standard developed by the World Wide Web Consortium (W3C) since 1999.

(…)

All major modern web browsers (…) have at least some degree of support for SVG and can render the markup directly.
Wikipedia

That’s enough for me. Let’s see how to install it.

Installing

First, I’m on OS X, so I’m going to use Homebrew. If you’re on Linux, use your favorite package manager. The first thing to install is Cairo, which can be done simply by:

Then install the gem:

Now you’re ready to use it. Just browse through the Rubyvis documentation or use the sample program to see if it’s working:

And you should be done. But how to view it outside of a browser (or if you want to post it somewhere, blabla)? Use the rsvg command line utility to convert svg to png. First, the gem:

In my system, I had a problem with libpixman. Apparently rsvg wasn’t looking my PATH correctly, so I just created a symlink:

And it worked:

Graphical test

Other solution

Rubyvis is obviously powerful enough to create almost anything I might need (ok, I have MATLAB for surface plots), but its syntax isn’t very… attractive. I want to create wrappers and make it easy enough to create common stuff, like in the R programming language — which I’ve been studying for the past weeks.

I found another project for graphics in Ruby, with support for JRuby, called Gruff. I didn’t have time to test it this week, but seems interesting enough. Maybe another day.

Leave a Reply