[Python-modules-commits] [python-mplexporter] 08/135: add vega examples
Wolfgang Borgert
debacle at moszumanska.debian.org
Tue Sep 23 21:18:58 UTC 2014
This is an automated email from the git hooks/post-receive script.
debacle pushed a commit to branch master
in repository python-mplexporter.
commit dc9e712956d7e886b203708a9268e4759a748e29
Author: Jake Vanderplas <vanderplas at astro.washington.edu>
Date: Mon Feb 17 10:42:19 2014 -0800
add vega examples
---
VegaTest.ipynb | 461 +++++++++++++++++++++++++++++++++
mplexporter/renderers/vega_renderer.py | 49 +++-
mplexporter/tools.py | 52 ++++
3 files changed, 553 insertions(+), 9 deletions(-)
diff --git a/VegaTest.ipynb b/VegaTest.ipynb
new file mode 100644
index 0000000..e631418
--- /dev/null
+++ b/VegaTest.ipynb
@@ -0,0 +1,461 @@
+{
+ "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": [
+ "<script>\n",
+ " if (window['d3'] === undefined) {\n",
+ " require.config({ paths: {d3: \"http://d3js.org/d3.v3.min\"} });\n",
+ " require([\"d3\"], function(d3) {\n",
+ " window.d3 = d3;\n",
+ " $.getScript(\"http://d3js.org/d3.geo.projection.v0.min.js\", function() {\n",
+ " $.getScript(\"http://wrobstory.github.io/d3-cloud/d3.layout.cloud.js\", function() {\n",
+ " $.getScript(\"http://d3js.org/topojson.v1.min.js\", function() {\n",
+ " $.getScript(\"http://trifacta.github.com/vega/vega.js\", function() {\n",
+ " $([IPython.events]).trigger(\"vega_loaded.vincent\");\n",
+ " })\n",
+ " })\n",
+ " })\n",
+ " });\n",
+ " });\n",
+ " };\n",
+ " if (window['topojson'] === undefined) {\n",
+ " require.config(\n",
+ " { paths: {topojson: \"http://d3js.org/topojson.v1.min\"} }\n",
+ " );\n",
+ " require([\"topojson\"], function(topojson) {\n",
+ " window.topojson = topojson;\n",
+ " });\n",
+ " };\n",
+ " </script>"
+ ],
+ "metadata": {},
+ "output_type": "display_data",
+ "text": [
+ "<IPython.core.display.HTML at 0x10258be10>"
+ ]
+ }
+ ],
+ "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",
+ "vega_fig = fig_to_vega(fig)\n",
+ "vega_fig"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "html": [
+ "<div id=\"vis9884\"></div><script>\n",
+ "\n",
+ "( function() {\n",
+ " var _do_plot = function() {\n",
+ " if ( (typeof vg == 'undefined') && (typeof IPython != 'undefined')) {\n",
+ " $([IPython.events]).on(\"vega_loaded.vincent\", _do_plot);\n",
+ " return;\n",
+ " }\n",
+ " vg.parse.spec({\"scales\": [{\"range\": \"width\", \"title\": \"x\", \"type\": \"linear\", \"domain\": [0.0, 10.0], \"name\": \"x\"}, {\"range\": \"height\", \"title\": \"sine and cosine\", \"type\": \"linear\", \"domain\": [-1.3999999999999999, 1.3999999999999999], \"name\": \"y\"}], \"axes\": [{\"ticks\": 10, \"type\": \"x\", \"scale\": \"x\"}, {\"ticks\": 10, \"type\": \"y\", \"scale\": \"y\"}], \"height\": 320, \"width\": 480, \"marks\": [{\"from\": {\"data\": \"table001 [...]
+ " chart({el: \"#vis9884\"}).update();\n",
+ " });\n",
+ " };\n",
+ " _do_plot();\n",
+ "})();\n",
+ "</script>\n"
+ ],
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 3,
+ "text": [
+ "<mplexporter.renderers.vega_renderer.VegaRenderer at 0x10610ca90>"
+ ]
+ }
+ ],
+ "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": [
+ "dict(vega_fig)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 4,
+ "text": [
+ "{'axes': [{'scale': 'x', 'ticks': 10, 'type': 'x'},\n",
+ " {'scale': 'y', 'ticks': 10, 'type': 'y'}],\n",
+ " 'data': [{'name': 'table001',\n",
+ " 'values': [{'x': 0.0, 'y': 0.0},\n",
+ " {'x': 0.10101010101010101, 'y': 0.10083842025810461},\n",
+ " {'x': 0.20202020202020202, 'y': 0.20064885652268541},\n",
+ " {'x': 0.30303030303030304, 'y': 0.2984138044476411},\n",
+ " {'x': 0.40404040404040403, 'y': 0.39313661214832979},\n",
+ " {'x': 0.50505050505050508, 'y': 0.48385164043793466},\n",
+ " {'x': 0.60606060606060608, 'y': 0.56963410690896565},\n",
+ " {'x': 0.70707070707070707, 'y': 0.6496095135057065},\n",
+ " {'x': 0.80808080808080807, 'y': 0.72296256147946036},\n",
+ " {'x': 0.90909090909090906, 'y': 0.78894546284425737},\n",
+ " {'x': 1.0101010101010102, 'y': 0.84688556360298339},\n",
+ " {'x': 1.1111111111111112, 'y': 0.89619220102995634},\n",
+ " {'x': 1.2121212121212122, 'y': 0.93636272510428475},\n",
+ " {'x': 1.3131313131313131, 'y': 0.96698762270929961},\n",
+ " {'x': 1.4141414141414141, 'y': 0.98775469236008384},\n",
+ " {'x': 1.5151515151515151, 'y': 0.99845222690038948},\n",
+ " {'x': 1.6161616161616161, 'y': 0.99897117172335681},\n",
+ " {'x': 1.7171717171717171, 'y': 0.98930623651434013},\n",
+ " {'x': 1.8181818181818181, 'y': 0.96955594918232368},\n",
+ " {'x': 1.9191919191919191, 'y': 0.93992165143013118},\n",
+ " {'x': 2.0202020202020203, 'y': 0.90070544620295545},\n",
+ " {'x': 2.1212121212121211, 'y': 0.85230711793967529},\n",
+ " {'x': 2.2222222222222223, 'y': 0.7952200570230491},\n",
+ " {'x': 2.3232323232323231, 'y': 0.73002622997644639},\n",
+ " {'x': 2.4242424242424243, 'y': 0.65739024668277546},\n",
+ " {'x': 2.5252525252525251, 'y': 0.5780525851065732},\n",
+ " {'x': 2.6262626262626263, 'y': 0.49282204258892348},\n",
+ " {'x': 2.7272727272727271, 'y': 0.40256749066949654},\n",
+ " {'x': 2.8282828282828283, 'y': 0.30820901749007684},\n",
+ " {'x': 2.9292929292929291, 'y': 0.21070854807719289},\n",
+ " {'x': 3.0303030303030303, 'y': 0.11106003812412972},\n",
+ " {'x': 3.131313131313131, 'y': 0.010279341240534697},\n",
+ " {'x': 3.2323232323232323, 'y': -0.090606147033407733},\n",
+ " {'x': 3.3333333333333335, 'y': -0.19056796287548539},\n",
+ " {'x': 3.4343434343434343, 'y': -0.28858705872043244},\n",
+ " {'x': 3.5353535353535355, 'y': -0.38366419180611233},\n",
+ " {'x': 3.6363636363636362, 'y': -0.47483011082223953},\n",
+ " {'x': 3.7373737373737375, 'y': -0.56115543681520175},\n",
+ " {'x': 3.8383838383838382, 'y': -0.64176013761938777},\n",
+ " {'x': 3.9393939393939394, 'y': -0.71582249922919017},\n",
+ " {'x': 4.0404040404040407, 'y': -0.78258750265420218},\n",
+ " {'x': 4.141414141414141, 'y': -0.84137452086087006},\n",
+ " {'x': 4.2424242424242422, 'y': -0.89158425733514024},\n",
+ " {'x': 4.3434343434343434, 'y': -0.93270485553183358},\n",
+ " {'x': 4.4444444444444446, 'y': -0.96431711692877831},\n",
+ " {'x': 4.545454545454545, 'y': -0.98609877449092964},\n",
+ " {'x': 4.6464646464646462, 'y': -0.99782777797921263},\n",
+ " {'x': 4.7474747474747474, 'y': -0.99938455761243572},\n",
+ " {'x': 4.8484848484848486, 'y': -0.99075324300567713},\n",
+ " {'x': 4.9494949494949498, 'y': -0.97202182495883338},\n",
+ " {'x': 5.0505050505050502, 'y': -0.94338125844599963},\n",
+ " {'x': 5.1515151515151514, 'y': -0.90512351595013674},\n",
+ " {'x': 5.2525252525252526, 'y': -0.85763861098805172},\n",
+ " {'x': 5.3535353535353538, 'y': -0.80141062216896974},\n",
+ " {'x': 5.4545454545454541, 'y': -0.73701275831891344},\n",
+ " {'x': 5.5555555555555554, 'y': -0.66510151497882242},\n",
+ " {'x': 5.6565656565656566, 'y': -0.58640998184723503},\n",
+ " {'x': 5.7575757575757578, 'y': -0.50174036939391131},\n",
+ " {'x': 5.8585858585858581, 'y': -0.41195583083086279},\n",
+ " {'x': 5.9595959595959593, 'y': -0.31797166281061867},\n",
+ " {'x': 6.0606060606060606, 'y': -0.22074597455506337},\n",
+ " {'x': 6.1616161616161618, 'y': -0.12126992053716677},\n",
+ " {'x': 6.2626262626262621, 'y': -0.020557596287260064},\n",
+ " {'x': 6.3636363636363633, 'y': 0.080364299670281733},\n",
+ " {'x': 6.4646464646464645, 'y': 0.18046693235991093},\n",
+ " {'x': 6.5656565656565657, 'y': 0.27872981867755731},\n",
+ " {'x': 6.666666666666667, 'y': 0.37415123057121996},\n",
+ " {'x': 6.7676767676767673, 'y': 0.46575840702565169},\n",
+ " {'x': 6.8686868686868685, 'y': 0.55261747074640588},\n",
+ " {'x': 6.9696969696969697, 'y': 0.63384294844890576},\n",
+ " {'x': 7.0707070707070709, 'y': 0.70860679769921819},\n",
+ " {'x': 7.1717171717171713, 'y': 0.77614684828358049},\n",
+ " {'x': 7.2727272727272725, 'y': 0.83577457205225891},\n",
+ " {'x': 7.3737373737373737, 'y': 0.8868821020290788},\n",
+ " {'x': 7.4747474747474749, 'y': 0.92894842923125132},\n",
+ " {'x': 7.5757575757575752, 'y': 0.96154471402682351},\n",
+ " {'x': 7.6767676767676765, 'y': 0.98433865788382358},\n",
+ " {'x': 7.7777777777777777, 'y': 0.9970978909438748},\n",
+ " {'x': 7.8787878787878789, 'y': 0.9996923408861117},\n",
+ " {'x': 7.9797979797979792, 'y': 0.99209555893232282},\n",
+ " {'x': 8.0808080808080813, 'y': 0.97438498947553576},\n",
+ " {'x': 8.1818181818181817, 'y': 0.94674118058335432},\n",
+ " {'x': 8.282828282828282, 'y': 0.90944594342446261},\n",
+ " {'x': 8.3838383838383841, 'y': 0.86287947938178355},\n",
+ " {'x': 8.4848484848484844, 'y': 0.80751650413956255},\n",
+ " {'x': 8.5858585858585865, 'y': 0.74392140825684439},\n",
+ " {'x': 8.6868686868686869, 'y': 0.67274250356226473},\n",
+ " {'x': 8.7878787878787872, 'y': 0.59470541402449739},\n",
+ " {'x': 8.8888888888888893, 'y': 0.51060567847428295},\n",
+ " {'x': 8.9898989898989896, 'y': 0.42130064058860689},\n",
+ " {'x': 9.0909090909090899, 'y': 0.32770070881349983},\n",
+ " {'x': 9.191919191919192, 'y': 0.23076007532505177},\n",
+ " {'x': 9.2929292929292924, 'y': 0.13146698864295842},\n",
+ " {'x': 9.3939393939393945, 'y': 0.03083367906114098},\n",
+ " {'x': 9.4949494949494948, 'y': -0.070113960400646769},\n",
+ " {'x': 9.5959595959595951, 'y': -0.1703468323280965},\n",
+ " {'x': 9.6969696969696972, 'y': -0.26884312591038401},\n",
+ " {'x': 9.7979797979797976, 'y': -0.36459873365588863},\n",
+ " {'x': 9.8989898989898997, 'y': -0.45663748763377376},\n",
+ " {'x': 10.0, 'y': -0.54402111088936989}]},\n",
+ " {'name': 'table002',\n",
+ " 'values': [{'x': 0.0, 'y': 1.0},\n",
+ " {'x': 0.10101010101010101, 'y': 0.99490281585683027},\n",
+ " {'x': 0.20202020202020202, 'y': 0.97966322599969979},\n",
+ " {'x': 0.30303030303030304, 'y': 0.95443658842014489},\n",
+ " {'x': 0.40404040404040403, 'y': 0.91948007275227761},\n",
+ " {'x': 0.50505050505050508, 'y': 0.8751500385908233},\n",
+ " {'x': 0.60606060606060608, 'y': 0.82189840263017},\n",
+ " {'x': 0.70707070707070707, 'y': 0.76026803165915058},\n",
+ " {'x': 0.80808080808080807, 'y': 0.69088720837706741},\n",
+ " {'x': 0.90909090909090906, 'y': 0.6144632264484674},\n",
+ " {'x': 1.0101010101010102, 'y': 0.53177518009103919},\n",
+ " {'x': 1.1111111111111112, 'y': 0.44366602170222852},\n",
+ " {'x': 1.2121212121212122, 'y': 0.35103396849205021},\n",
+ " {'x': 1.3131313131313131, 'y': 0.25482334572604864},\n",
+ " {'x': 1.4141414141414141, 'y': 0.15601495992575853},\n",
+ " {'x': 1.5151515151515151, 'y': 0.055616100165806738},\n",
+ " {'x': 1.6161616161616161, 'y': -0.045349730601885241},\n",
+ " {'x': 1.7171717171717171, 'y': -0.14585324951413534},\n",
+ " {'x': 1.8181818181818181, 'y': -0.24486988668507895},\n",
+ " {'x': 1.9191919191919191, 'y': -0.34139023004892061},\n",
+ " {'x': 2.0202020202020203, 'y': -0.43443031567828566},\n",
+ " {'x': 2.1212121212121211, 'y': -0.52304165867487507},\n",
+ " {'x': 2.2222222222222223, 'y': -0.60632092237383539},\n",
+ " {'x': 2.3232323232323231, 'y': -0.68341912729040333},\n",
+ " {'x': 2.4242424242424243, 'y': -0.75355030592944461},\n",
+ " {'x': 2.5252525252525251, 'y': -0.81599951522755698},\n",
+ " {'x': 2.6262626262626263, 'y': -0.87013012494596542},\n",
+ " {'x': 2.7272727272727271, 'y': -0.91539030771363583},\n",
+ " {'x': 2.8282828282828283, 'y': -0.95131866455872793},\n",
+ " {'x': 2.9292929292929291, 'y': -0.9775489285796396},\n",
+ " {'x': 3.0303030303030303, 'y': -0.99381369880469395},\n",
+ " {'x': 3.131313131313131, 'y': -0.99994716617612389},\n",
+ " {'x': 3.2323232323232323, 'y': -0.9958868038686729},\n",
+ " {'x': 3.3333333333333335, 'y': -0.98167400471107902},\n",
+ " {'x': 3.4343434343434343, 'y': -0.95745365921233472},\n",
+ " {'x': 3.5353535353535355, 'y': -0.92347267849447645},\n",
+ " {'x': 3.6363636363636362, 'y': -0.88007747718967322},\n",
+ " {'x': 3.7373737373737375, 'y': -0.8277104419618857},\n",
+ " {'x': 3.8383838383838382, 'y': -0.7669054216542901},\n",
+ " {'x': 3.9393939393939394, 'y': -0.69828228503755996},\n",
+ " {'x': 4.0404040404040407, 'y': -0.62254060163933012},\n",
+ " {'x': 4.141414141414141, 'y': -0.54045251007479034},\n",
+ " {'x': 4.2424242424242422, 'y': -0.45285484658127084},\n",
+ " {'x': 4.3434343434343434, 'y': -0.36064061400144803},\n",
+ " {'x': 4.4444444444444446, 'y': -0.26474987818348289},\n",
+ " {'x': 4.545454545454545, 'y': -0.16616018460355267},\n",
+ " {'x': 4.6464646464646462, 'y': -0.065876592907246775},\n",
+ " {'x': 4.7474747474747474, 'y': 0.035078569038604845},\n",
+ " {'x': 4.8484848484848486, 'y': 0.13567612713271912},\n",
+ " {'x': 4.9494949494949498, 'y': 0.23489055281917826},\n",
+ " {'x': 5.0505050505050502, 'y': 0.33171041770321597},\n",
+ " {'x': 5.1515151515151514, 'y': 0.42514870442477243},\n",
+ " {'x': 5.2525252525252526, 'y': 0.51425286867696263},\n",
+ " {'x': 5.3535353535353538, 'y': 0.59811454979355327},\n",
+ " {'x': 5.4545454545454541, 'y': 0.67587883091212952},\n",
+ " {'x': 5.5555555555555554, 'y': 0.74675295431144773},\n",
+ " {'x': 5.6565656565656566, 'y': 0.81001440307560302},\n",
+ " {'x': 5.7575757575757578, 'y': 0.86501826669756599},\n",
+ " {'x': 5.8585858585858581, 'y': 0.91120381553440255},\n",
+ " {'x': 5.9595959595959593, 'y': 0.94810021709176406},\n",
+ " {'x': 6.0606060606060606, 'y': 0.97533133586373366},\n",
+ " {'x': 6.1616161616161618, 'y': 0.99261956779670091},\n",
+ " {'x': 6.2626262626262621, 'y': 0.99978867028732132},\n",
+ " {'x': 6.3636363636363633, 'y': 0.99676555886452312},\n",
+ " {'x': 6.4646464646464645, 'y': 0.98358105223952097},\n",
+ " {'x': 6.5656565656565657, 'y': 0.96036955812852376},\n",
+ " {'x': 6.666666666666667, 'y': 0.92736770305097527},\n",
+ " {'x': 6.7676767676767673, 'y': 0.88491192007166875},\n",
+ " {'x': 6.8686868686868685, 'y': 0.83343501907817941},\n",
+ " {'x': 6.9696969696969697, 'y': 0.77346177455747467},\n",
+ " {'x': 7.0707070707070709, 'y': 0.70560357585152533},\n",
+ " {'x': 7.1717171717171713, 'y': 0.63055219442918808},\n",
+ " {'x': 7.2727272727272725, 'y': 0.54907273171307958},\n",
+ " {'x': 7.3737373737373737, 'y': 0.46199581935390133},\n",
+ " {'x': 7.4747474747474749, 'y': 0.37020915146548028},\n",
+ " {'x': 7.5757575757575752, 'y': 0.27464843514404769},\n",
+ " {'x': 7.6767676767676765, 'y': 0.17628785152548898},\n",
+ " {'x': 7.7777777777777777, 'y': 0.076130124624071926},\n",
+ " {'x': 7.8787878787878789, 'y': -0.024803700805447838},\n",
+ " {'x': 7.9797979797979792, 'y': -0.12548466817409182},\n",
+ " {'x': 8.0808080808080813, 'y': -0.22488639862108173},\n",
+ " {'x': 8.1818181818181817, 'y': -0.32199555429793802},\n",
+ " {'x': 8.282828282828282, 'y': -0.41582216870771721},\n",
+ " {'x': 8.3838383838383841, 'y': -0.50540973878806716},\n",
+ " {'x': 8.4848484848484844, 'y': -0.58984497585570728},\n",
+ " {'x': 8.5858585858585865, 'y': -0.66826711600762867},\n",
+ " {'x': 8.6868686868686869, 'y': -0.73987669506531706},\n",
+ " {'x': 8.7878787878787872, 'y': -0.80394369860703008},\n",
+ " {'x': 8.8888888888888893, 'y': -0.85981500400366195},\n",
+ " {'x': 8.9898989898989896, 'y': -0.90692103859135909},\n",
+ " {'x': 9.0909090909090899, 'y': -0.94478158610502661},\n",
+ " {'x': 9.191919191919192, 'y': -0.97301068217978803},\n",
+ " {'x': 9.2929292929292924, 'y': -0.99132054901386579},\n",
+ " {'x': 9.3939393939393945, 'y': -0.99952452908148015},\n",
+ " {'x': 9.4949494949494948, 'y': -0.99753898798840768},\n",
+ " {'x': 9.5959595959595951, 'y': -0.9853841670717991},\n",
+ " {'x': 9.6969696969696972, 'y': -0.96318397705253245},\n",
+ " {'x': 9.7979797979797976, 'y': -0.93116473484369155},\n",
+ " {'x': 9.8989898989898997, 'y': -0.88965285639260161},\n",
+ " {'x': 10.0, 'y': -0.83907152907645244}]},\n",
+ " {'name': 'table003',\n",
+ " 'values': [{'x': 0.0, 'y': 0.0},\n",
+ " {'x': 0.50505050505050508, 'y': 0.48385164043793466},\n",
+ " {'x': 1.0101010101010102, 'y': 0.84688556360298339},\n",
+ " {'x': 1.5151515151515151, 'y': 0.99845222690038948},\n",
+ " {'x': 2.0202020202020203, 'y': 0.90070544620295545},\n",
+ " {'x': 2.5252525252525251, 'y': 0.5780525851065732},\n",
+ " {'x': 3.0303030303030303, 'y': 0.11106003812412972},\n",
+ " {'x': 3.5353535353535355, 'y': -0.38366419180611233},\n",
+ " {'x': 4.0404040404040407, 'y': -0.78258750265420218},\n",
+ " {'x': 4.545454545454545, 'y': -0.98609877449092964},\n",
+ " {'x': 5.0505050505050502, 'y': -0.94338125844599963},\n",
+ " {'x': 5.5555555555555554, 'y': -0.66510151497882242},\n",
+ " {'x': 6.0606060606060606, 'y': -0.22074597455506337},\n",
+ " {'x': 6.5656565656565657, 'y': 0.27872981867755731},\n",
+ " {'x': 7.0707070707070709, 'y': 0.70860679769921819},\n",
+ " {'x': 7.5757575757575752, 'y': 0.96154471402682351},\n",
+ " {'x': 8.0808080808080813, 'y': 0.97438498947553576},\n",
+ " {'x': 8.5858585858585865, 'y': 0.74392140825684439},\n",
+ " {'x': 9.0909090909090899, 'y': 0.32770070881349983},\n",
+ " {'x': 9.5959595959595951, 'y': -0.1703468323280965}]},\n",
+ " {'name': 'table004',\n",
+ " 'values': [{'x': 0.0, 'y': 1.0},\n",
+ " {'x': 0.50505050505050508, 'y': 0.8751500385908233},\n",
+ " {'x': 1.0101010101010102, 'y': 0.53177518009103919},\n",
+ " {'x': 1.5151515151515151, 'y': 0.055616100165806738},\n",
+ " {'x': 2.0202020202020203, 'y': -0.43443031567828566},\n",
+ " {'x': 2.5252525252525251, 'y': -0.81599951522755698},\n",
+ " {'x': 3.0303030303030303, 'y': -0.99381369880469395},\n",
+ " {'x': 3.5353535353535355, 'y': -0.92347267849447645},\n",
+ " {'x': 4.0404040404040407, 'y': -0.62254060163933012},\n",
+ " {'x': 4.545454545454545, 'y': -0.16616018460355267},\n",
+ " {'x': 5.0505050505050502, 'y': 0.33171041770321597},\n",
+ " {'x': 5.5555555555555554, 'y': 0.74675295431144773},\n",
+ " {'x': 6.0606060606060606, 'y': 0.97533133586373366},\n",
+ " {'x': 6.5656565656565657, 'y': 0.96036955812852376},\n",
+ " {'x': 7.0707070707070709, 'y': 0.70560357585152533},\n",
+ " {'x': 7.5757575757575752, 'y': 0.27464843514404769},\n",
+ " {'x': 8.0808080808080813, 'y': -0.22488639862108173},\n",
+ " {'x': 8.5858585858585865, 'y': -0.66826711600762867},\n",
+ " {'x': 9.0909090909090899, 'y': -0.94478158610502661},\n",
+ " {'x': 9.5959595959595951, 'y': -0.9853841670717991}]}],\n",
+ " 'height': 320,\n",
+ " 'marks': [{'from': {'data': 'table001'},\n",
+ " 'properties': {'enter': {'interpolate': {'value': 'monotone'},\n",
+ " 'stroke': {'value': '#FF0000'},\n",
+ " 'strokeOpacity': {'value': 0.5},\n",
+ " 'strokeWidth': {'value': 3},\n",
+ " 'x': {'field': 'data.x', 'scale': 'x'},\n",
+ " 'y': {'field': 'data.y', 'scale': 'y'}}},\n",
+ " 'type': 'line'},\n",
+ " {'from': {'data': 'table002'},\n",
+ " 'properties': {'enter': {'interpolate': {'value': 'monotone'},\n",
+ " 'stroke': {'value': '#0000FF'},\n",
+ " 'strokeOpacity': {'value': 0.5},\n",
+ " 'strokeWidth': {'value': 3},\n",
+ " 'x': {'field': 'data.x', 'scale': 'x'},\n",
+ " 'y': {'field': 'data.y', 'scale': 'y'}}},\n",
+ " 'type': 'line'},\n",
+ " {'from': {'data': 'table003'},\n",
+ " 'properties': {'enter': {'fill': {'value': '#FF0000'},\n",
+ " 'fillOpacity': {'value': 0.5},\n",
+ " 'interpolate': {'value': 'monotone'},\n",
+ " 'stroke': {'value': '#000000'},\n",
+ " 'strokeOpacity': {'value': 0.5},\n",
+ " 'strokeWidth': {'value': 0.5},\n",
+ " 'x': {'field': 'data.x', 'scale': 'x'},\n",
+ " 'y': {'field': 'data.y', 'scale': 'y'}}},\n",
+ " 'type': 'symbol'},\n",
+ " {'from': {'data': 'table004'},\n",
+ " 'properties': {'enter': {'fill': {'value': '#0000FF'},\n",
+ " 'fillOpacity': {'value': 0.5},\n",
+ " 'interpolate': {'value': 'monotone'},\n",
+ " 'stroke': {'value': '#000000'},\n",
+ " 'strokeOpacity': {'value': 0.5},\n",
+ " 'strokeWidth': {'value': 0.5},\n",
+ " 'x': {'field': 'data.x', 'scale': 'x'},\n",
+ " 'y': {'field': 'data.y', 'scale': 'y'}}},\n",
+ " 'type': 'symbol'}],\n",
+ " 'scales': [{'domain': (0.0, 10.0),\n",
+ " 'name': 'x',\n",
+ " 'range': 'width',\n",
+ " 'title': 'x',\n",
+ " 'type': 'linear'},\n",
+ " {'domain': (-1.3999999999999999, 1.3999999999999999),\n",
+ " 'name': 'y',\n",
+ " 'range': 'height',\n",
+ " 'title': 'sine and cosine',\n",
+ " 'type': 'linear'}],\n",
+ " 'width': 480}"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/mplexporter/renderers/vega_renderer.py b/mplexporter/renderers/vega_renderer.py
index e505f41..b20782a 100644
--- a/mplexporter/renderers/vega_renderer.py
+++ b/mplexporter/renderers/vega_renderer.py
@@ -1,5 +1,6 @@
import warnings
import json
+import random
from .base import Renderer
from ..exporter import Exporter
@@ -78,18 +79,48 @@ class VegaRenderer(Renderer):
}
})
- def dict(self):
- return dict(width=self.figwidth,
- height=self.figheight,
- data=self.data,
- scales=self.scales,
- axes=self.axes,
- marks=self.marks)
+ def to_dict(self):
+ return dict(self)
+
+ def to_json(self, *args, **kwargs):
+ return json.dumps(self.to_dict(), *args, **kwargs)
+
+ def __iter__(self):
+ yield "width", self.figwidth
+ yield "height", self.figheight,
+ yield "data", self.data,
+ yield "scales", self.scales
+ yield "axes", self.axes
+ yield "marks", self.marks
+
+ def _repr_html_(self):
+ """Build the HTML representation for IPython."""
+ id = random.randint(0, 2 ** 16)
+ html = '<div id="vis%d"></div>' % id
+ html += '<script>\n'
+ html += VEGA_TEMPLATE % (self.to_json(), id)
+ html += '</script>\n'
+ return html
def fig_to_vega(fig):
"""Convert a matplotlib figure to vega dictionary"""
renderer = VegaRenderer()
Exporter(renderer).run(fig)
- return renderer.dict()
-
+ return renderer
+
+
+VEGA_TEMPLATE = """
+( function() {
+ var _do_plot = function() {
+ if ( (typeof vg == 'undefined') && (typeof IPython != 'undefined')) {
+ $([IPython.events]).on("vega_loaded.vincent", _do_plot);
+ return;
+ }
+ vg.parse.spec(%s, function(chart) {
+ chart({el: "#vis%d"}).update();
+ });
+ };
+ _do_plot();
+})();
+"""
diff --git a/mplexporter/tools.py b/mplexporter/tools.py
new file mode 100644
index 0000000..551e8be
--- /dev/null
+++ b/mplexporter/tools.py
@@ -0,0 +1,52 @@
+"""
+Tools for matplotlib plot exporting
+"""
+
+
+def ipynb_vega_init():
+ """Initialize the IPython notebook display elements
+
+ This function borrows heavily from the excellent vincent package:
+ http://github.com/wrobstory/vincent
+ """
+ try:
+ from IPython.core.display import display, HTML
+ except ImportError:
+ print('IPython Notebook could not be loaded.')
+
+ require_js = '''
+ if (window['d3'] === undefined) {{
+ require.config({{ paths: {{d3: "http://d3js.org/d3.v3.min"}} }});
+ require(["d3"], function(d3) {{
+ window.d3 = d3;
+ {0}
+ }});
+ }};
+ if (window['topojson'] === undefined) {{
+ require.config(
+ {{ paths: {{topojson: "http://d3js.org/topojson.v1.min"}} }}
+ );
+ require(["topojson"], function(topojson) {{
+ window.topojson = topojson;
+ }});
+ }};
+ '''
+ d3_geo_projection_js_url = "http://d3js.org/d3.geo.projection.v0.min.js"
+ d3_layout_cloud_js_url = ("http://wrobstory.github.io/d3-cloud/"
+ "d3.layout.cloud.js")
+ topojson_js_url = "http://d3js.org/topojson.v1.min.js"
+ vega_js_url = 'http://trifacta.github.com/vega/vega.js'
+
+ dep_libs = '''$.getScript("%s", function() {
+ $.getScript("%s", function() {
+ $.getScript("%s", function() {
+ $.getScript("%s", function() {
+ $([IPython.events]).trigger("vega_loaded.vincent");
+ })
+ })
+ })
+ });''' % (d3_geo_projection_js_url, d3_layout_cloud_js_url,
+ topojson_js_url, vega_js_url)
+ load_js = require_js.format(dep_libs)
+ html = '<script>'+load_js+'</script>'
+ display(HTML(html))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-mplexporter.git
More information about the Python-modules-commits
mailing list