{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Matplotlib to Vega Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook contains some examples of converting matplotlib plots to vega." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from mplexporter.renderers import fig_to_vega" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "from mplexporter.tools import ipynb_vega_init\n", "ipynb_vega_init()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "fig, ax = plt.subplots()\n", "x = np.linspace(0, 10, 100)\n", "ax.plot(x, np.sin(x), '-r', alpha=0.5, lw=3)\n", "ax.plot(x, np.cos(x), '-b', alpha=0.5, lw=3)\n", "ax.plot(x[::5], np.sin(x[::5]), 'or', alpha=0.5)\n", "ax.plot(x[::5], np.cos(x[::5]), 'ob', alpha=0.5)\n", "\n", "ax.set_xlabel('x')\n", "ax.set_ylabel('sine and cosine')\n", "ax.set_xlim(0, 10)\n", "ax.set_ylim(-1.4, 1.4)\n", "\n", "fig_to_vega(fig, notebook=True)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's the dictionary which is converted to a vega JSON object\n", "in order to create this representation:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print fig_to_vega(fig)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "
\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }