{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IRuby and matplotlib (through PyCall) integration example\n",
    "\n",
    "## Install matplotlib\n",
    "\n",
    "You can install matplotlib by `gem install --pre matplotlib`.\n",
    "matplotlib.gem depends on pycall.gem so you don't need to install pycall explicitly.\n",
    "\n",
    "## How to use matplotlib in IRuby\n",
    "\n",
    "You should call `Matplotlib::IRuby.activate` method defined in `matplotlib/iruby` to prepare the integration between IRuby and matplotlib.\n",
    "\n",
    "This method defines `Matplotlib::Pyplot` module that has singleton methods connected to functions defined in `matplotlib.pyplot` module in Python-side.\n",
    "\n",
    "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.\n",
    "\n",
    "## Example code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Matplotlib::Pyplot"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "require 'matplotlib/iruby'\n",
    "Matplotlib::IRuby.activate\n",
    "plt = Matplotlib::Pyplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32204 characters]",
      "text/plain": [
       "#<Matplotlib::Figure:0x007f997699a658 @__pyptr__=#<PyCall::PyPtr:0x007f997699a6a8 type=Figure addr=0x0000010af9c4e0>>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "#<PyCall::List:0x007f99769a0648 @__pyptr__=#<PyCall::PyPtr:0x007f99769a06c0 type=list addr=0x0000010b09e848>>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32316 characters]",
      "text/plain": [
       "#<Matplotlib::Figure:0x007f9978122938 @__pyptr__=#<PyCall::PyPtr:0x007f9978122960 type=Figure addr=0x0000010afd6908>>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "#<PyCall::List:0x007f9978123b30 @__pyptr__=#<PyCall::PyPtr:0x007f9978123b58 type=list addr=0x0000010b4eef88>>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Multiple plots in a figure\n",
    "plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 1, 2])\n",
    "plt.plot([10, 20, 30, 40, 50], [1, 2, 3, 1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32204 characters]",
      "text/plain": [
       "#<Matplotlib::Figure:0x007f997985c9c8 @__pyptr__=#<PyCall::PyPtr:0x007f997985c9f0 type=Figure addr=0x0000010b398a58>>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 32260 characters]",
      "text/plain": [
       "#<Matplotlib::Figure:0x007f9979854958 @__pyptr__=#<PyCall::PyPtr:0x007f9979854980 type=Figure addr=0x0000010b4e2668>>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "#<PyCall::List:0x007f997985fe48 @__pyptr__=#<PyCall::PyPtr:0x007f997985ff10 type=list addr=0x0000010b831408>>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Multiple figures\n",
    "plt.figure()\n",
    "plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 1, 2])\n",
    "\n",
    "plt.figure()\n",
    "plt.plot([10, 20, 30, 40, 50], [1, 2, 3, 1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.4.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
