IRuby and matplotlib (through PyCall) integration example

Install matplotlib

You can install matplotlib by gem install --pre matplotlib. matplotlib.gem depends on pycall.gem so you don't need to install pycall explicitly.

How to use matplotlib in IRuby

You should call Matplotlib::IRuby.activate method defined in matplotlib/iruby to prepare the integration between IRuby and matplotlib.

This method defines Matplotlib::Pyplot module that has singleton methods connected to functions defined in matplotlib.pyplot module in Python-side.

Also, this method defines the way to display matplotlib's figure as an output of execution. When you display the current figure, you can call Matplotlib::Pyplot.gcf method at the end of the code cell.

Example code


In [1]:
require 'matplotlib/iruby'
Matplotlib::IRuby.activate
plt = Matplotlib::Pyplot


Out[1]:
Matplotlib::Pyplot

In [2]:
plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 1, 2])


Out[2]:
Out[2]:
#<PyCall::List:0x007f99769a0648 @__pyptr__=#<PyCall::PyPtr:0x007f99769a06c0 type=list addr=0x0000010b09e848>>

In [3]:
# Multiple plots in a figure
plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 1, 2])
plt.plot([10, 20, 30, 40, 50], [1, 2, 3, 1, 2])


Out[3]:
Out[3]:
#<PyCall::List:0x007f9978123b30 @__pyptr__=#<PyCall::PyPtr:0x007f9978123b58 type=list addr=0x0000010b4eef88>>

In [4]:
# Multiple figures
plt.figure()
plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 1, 2])

plt.figure()
plt.plot([10, 20, 30, 40, 50], [1, 2, 3, 1, 2])


Out[4]:
Out[4]:
Out[4]:
#<PyCall::List:0x007f997985fe48 @__pyptr__=#<PyCall::PyPtr:0x007f997985ff10 type=list addr=0x0000010b831408>>

In [ ]: