Building a plotting library for Ruby

Arafat Khan
Analytics Vidhya
Published in
4 min readNov 24, 2019

Hi Readers,

At Sciruby, we came across the ambitious task of designing the next Matplotlib for Ruby and so we examined quite a few different plotting libraries and figured that if we could put together ideas from all of these libraries then we can make something really amazing(Rubyplot).

Initial plans

Initially we came up with an elaborate plan to adopt ideas from Matplotlib. I made a proposal for Google Summer of code program and shared it with my mentors at Sciruby. We bounced around any ideas and I finally started working on the project….

Our first and the main question was….

How do we draw?

You see, there are many libraries in Ruby and python for doing small visualizations but making a whole plotting library was a task at a completely different level. We need something really flexible to make all things fast. So after some heavy discussions we ended deciding to use two different visualization libraries and merge them together at the end.

Image Magick and GR Framework

We decided to build two different ruby libraries independently but with many parallels in their code. Eventually when the project gets completed we will combine them into a single repository and give users the option to use either of the libraries as a backend for construction of the plots. Now, enough with the design talk.

Let’s make something real by starting off with Gr Plot

Gr plot is plotting library for ruby that uses the GR framework as a backend.

If you look at the code it’s easy to notice that not only can you draw the line curves but also easily specify the line width and the line colors etc pretty easily

g.line! @x1, @y1, line_width: 4, line_color: :green

Not only that, we also have a lot of customization features

def line!(x_coordinates, y_coordinates, line_width: :default,
line_color: :default, line_type: :default,
marker_size: :default, marker_color: :default,
marker_type: :default)

There are a lot of other plots that can be drawn with GRPlot.

  • scatter
  • line
  • bar
  • stacked_bar
  • candelstick
Bar chart, Candlestick chart, Line chart, Stacked Bar chart

Now let’s move on to MagickPlot

Magick plot is a plotting library that produces quality figures in a variety of hardcopy formats using RMagick as a backend.

The commented code above is self-explanatory and plain. Notice how, the many customization options available make it easier to work with your plots. You can easily define labels colors/plot size(in pixels) etc in the plot.

Magickplot is also a very cool library with many features similar to GRPlot but the internal implementations of both the libraries are radically different. We believe that depending on the use cases the users can find either of them more useful than the other one.

So our next goal is to merge them together and give users a simple API to switch back ends easily from GR Plot to Magick Plot…..

Let’s take an example

Heart curves made in Magick Plot and GR Plot

Notice that there is literally no difference in using these two different libraries after they have been combined

g = Rubyplot::Figure.new(backend: :magick)                       g.scatter! @x1, @y1, marker_color: :pink, label: :heart                       g.save('scatter.png')                                               g2 = Rubyplot::Figure.new(backend: :GR)                       g2.text_font = :bookman_light_italic                       g2.scatter! @x1, @y1, marker_color: :red                       g2.save('scatter2.png')

The above code sample highlights how the figure class can be neatly used to use two different backends.

Acknowledgements

Thanks to all my Mentors from Sciruby namely, Sameer Deshmukh, Pjotr Prins, Prasun Anand and John Woods.

Special thanks to Pranav Garg -> The lead developer of GR-Ruby and a student in Gsoc for Sciruby

Feature requests

My ultimate goal is to make this project similar to a matplotlib equivalent for Ruby with tons of really amazing customization features. I hope you find this interesting and useful. Feel free to try it out from github. Any suggestions and recommendations are welcome.

--

--