{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Vincent Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook shows a simple example of using the vincent renderer to \n",
"convert a simple matplotlib plot to [vega](http://trifacta.github.io/vega/)\n",
"via the [vincent](https://vincent.readthedocs.org) package.\n",
"\n",
"This converter is still very incomplete, and will be improved with time."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import vincent\n",
"vincent.core.initialize_notebook()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
""
],
"metadata": {},
"output_type": "display_data",
"text": [
""
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import display, HTML\n",
"from vincent.visualization import _vega_t\n",
"import random\n",
"\n",
"def display_natural(obj):\n",
" \"\"\"\n",
" Display a vincent object in IPython\n",
" without forcing it to 100% of the page width\n",
" \"\"\"\n",
" # the following is adapted from vincent.Visualization._repr_html_\n",
" id = random.randint(0, 2 ** 16)\n",
" html = '' % id\n",
" html += '\\n'\n",
" return HTML(html)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from mplexporter.renderers import fig_to_vincent"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = np.linspace(0, 10, 100)\n",
"y = np.sin(x)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(x, y)\n",
"\n",
"display_natural(fig_to_vincent(fig))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
""
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.random.seed(0)\n",
"x = np.random.normal(size=50)\n",
"y = np.random.normal(size=50)\n",
"\n",
"fig, ax = plt.subplots()\n",
"ax.plot(x, y, 'or')\n",
"\n",
"display_natural(fig_to_vincent(fig))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
""
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}