This notebook contains some examples of converting matplotlib plots to vega.
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mplexporter.renderers import fig_to_vega
In [2]:
from mplexporter.tools import ipynb_vega_init
ipynb_vega_init()
In [3]:
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x), '-r', alpha=0.5, lw=3)
ax.plot(x, np.cos(x), '-b', alpha=0.5, lw=3)
ax.plot(x[::5], np.sin(x[::5]), 'or', alpha=0.5)
ax.plot(x[::5], np.cos(x[::5]), 'ob', alpha=0.5)
ax.set_xlabel('x')
ax.set_ylabel('sine and cosine')
ax.set_xlim(0, 10)
ax.set_ylim(-1.4, 1.4)
fig_to_vega(fig, notebook=True)
Out[3]:
Here's the dictionary which is converted to a vega JSON object in order to create this representation:
In [4]:
print fig_to_vega(fig)
<div id="vis41930"></div><script>
( function() {
var _do_plot = function() {
if ( (typeof vg == 'undefined') && (typeof IPython != 'undefined')) {
$([IPython.events]).on("vega_loaded.vincent", _do_plot);
return;
}
vg.parse.spec({"width": 480, "scales": [{"range": "width", "type": "linear", "domain": [0.0, 10.0], "name": "x"}, {"range": "height", "type": "linear", "domain": [-1.3999999999999999, 1.3999999999999999], "name": "y"}], "marks": [{"from": {"data": "table001"}, "type": "line", "properties": {"enter": {"strokeWidth": {"value": 3}, "strokeOpacity": {"value": 0.5}, "stroke": {"value": "#FF0000"}, "interpolate": {"value": "monotone"}, "y": {"field": "data.y", "scale": "y"}, "x": {"field": "data.x", "scale": "x"}}}}, {"from": {"data": "table002"}, "type": "line", "properties": {"enter": {"strokeWidth": {"value": 3}, "strokeOpacity": {"value": 0.5}, "stroke": {"value": "#0000FF"}, "interpolate": {"value": "monotone"}, "y": {"field": "data.y", "scale": "y"}, "x": {"field": "data.x", "scale": "x"}}}}, {"from": {"data": "table003"}, "type": "symbol", "properties": {"enter": {"stroke": {"value": "#000000"}, "strokeWidth": {"value": 0.5}, "fillOpacity": {"value": 0.5}, "y": {"field": "data.y", "scale": "y"}, "x": {"field": "data.x", "scale": "x"}, "strokeOpacity": {"value": 0.5}, "interpolate": {"value": "monotone"}, "fill": {"value": "#FF0000"}}}}, {"from": {"data": "table004"}, "type": "symbol", "properties": {"enter": {"stroke": {"value": "#000000"}, "strokeWidth": {"value": 0.5}, "fillOpacity": {"value": 0.5}, "y": {"field": "data.y", "scale": "y"}, "x": {"field": "data.x", "scale": "x"}, "strokeOpacity": {"value": 0.5}, "interpolate": {"value": "monotone"}, "fill": {"value": "#0000FF"}}}}], "axes": [{"ticks": 10, "type": "x", "scale": "x", "title": "x"}, {"ticks": 10, "type": "y", "scale": "y", "title": "sine and cosine"}], "data": [{"values": [{"y": 0.0, "x": 0.0}, {"y": 0.10083842025810461, "x": 0.10101010101010101}, {"y": 0.20064885652268541, "x": 0.20202020202020202}, {"y": 0.2984138044476411, "x": 0.30303030303030304}, {"y": 0.39313661214832979, "x": 0.40404040404040403}, {"y": 0.48385164043793466, "x": 0.50505050505050508}, {"y": 0.56963410690896565, "x": 0.60606060606060608}, {"y": 0.6496095135057065, "x": 0.70707070707070707}, {"y": 0.72296256147946036, "x": 0.80808080808080807}, {"y": 0.78894546284425737, "x": 0.90909090909090906}, {"y": 0.84688556360298339, "x": 1.0101010101010102}, {"y": 0.89619220102995634, "x": 1.1111111111111112}, {"y": 0.93636272510428475, "x": 1.2121212121212122}, {"y": 0.96698762270929961, "x": 1.3131313131313131}, {"y": 0.98775469236008384, "x": 1.4141414141414141}, {"y": 0.99845222690038948, "x": 1.5151515151515151}, {"y": 0.99897117172335681, "x": 1.6161616161616161}, {"y": 0.98930623651434013, "x": 1.7171717171717171}, {"y": 0.96955594918232368, "x": 1.8181818181818181}, {"y": 0.93992165143013118, "x": 1.9191919191919191}, {"y": 0.90070544620295545, "x": 2.0202020202020203}, {"y": 0.85230711793967529, "x": 2.1212121212121211}, {"y": 0.7952200570230491, "x": 2.2222222222222223}, {"y": 0.73002622997644639, "x": 2.3232323232323231}, {"y": 0.65739024668277546, "x": 2.4242424242424243}, {"y": 0.5780525851065732, "x": 2.5252525252525251}, {"y": 0.49282204258892348, "x": 2.6262626262626263}, {"y": 0.40256749066949654, "x": 2.7272727272727271}, {"y": 0.30820901749007684, "x": 2.8282828282828283}, {"y": 0.21070854807719289, "x": 2.9292929292929291}, {"y": 0.11106003812412972, "x": 3.0303030303030303}, {"y": 0.010279341240534697, "x": 3.131313131313131}, {"y": -0.090606147033407733, "x": 3.2323232323232323}, {"y": -0.19056796287548539, "x": 3.3333333333333335}, {"y": -0.28858705872043244, "x": 3.4343434343434343}, {"y": -0.38366419180611233, "x": 3.5353535353535355}, {"y": -0.47483011082223953, "x": 3.6363636363636362}, {"y": -0.56115543681520175, "x": 3.7373737373737375}, {"y": -0.64176013761938777, "x": 3.8383838383838382}, {"y": -0.71582249922919017, "x": 3.9393939393939394}, {"y": -0.78258750265420218, "x": 4.0404040404040407}, {"y": -0.84137452086087006, "x": 4.141414141414141}, {"y": -0.89158425733514024, "x": 4.2424242424242422}, {"y": -0.93270485553183358, "x": 4.3434343434343434}, {"y": -0.96431711692877831, "x": 4.4444444444444446}, {"y": -0.98609877449092964, "x": 4.545454545454545}, {"y": -0.99782777797921263, "x": 4.6464646464646462}, {"y": -0.99938455761243572, "x": 4.7474747474747474}, {"y": -0.99075324300567713, "x": 4.8484848484848486}, {"y": -0.97202182495883338, "x": 4.9494949494949498}, {"y": -0.94338125844599963, "x": 5.0505050505050502}, {"y": -0.90512351595013674, "x": 5.1515151515151514}, {"y": -0.85763861098805172, "x": 5.2525252525252526}, {"y": -0.80141062216896974, "x": 5.3535353535353538}, {"y": -0.73701275831891344, "x": 5.4545454545454541}, {"y": -0.66510151497882242, "x": 5.5555555555555554}, {"y": -0.58640998184723503, "x": 5.6565656565656566}, {"y": -0.50174036939391131, "x": 5.7575757575757578}, {"y": -0.41195583083086279, "x": 5.8585858585858581}, {"y": -0.31797166281061867, "x": 5.9595959595959593}, {"y": -0.22074597455506337, "x": 6.0606060606060606}, {"y": -0.12126992053716677, "x": 6.1616161616161618}, {"y": -0.020557596287260064, "x": 6.2626262626262621}, {"y": 0.080364299670281733, "x": 6.3636363636363633}, {"y": 0.18046693235991093, "x": 6.4646464646464645}, {"y": 0.27872981867755731, "x": 6.5656565656565657}, {"y": 0.37415123057121996, "x": 6.666666666666667}, {"y": 0.46575840702565169, "x": 6.7676767676767673}, {"y": 0.55261747074640588, "x": 6.8686868686868685}, {"y": 0.63384294844890576, "x": 6.9696969696969697}, {"y": 0.70860679769921819, "x": 7.0707070707070709}, {"y": 0.77614684828358049, "x": 7.1717171717171713}, {"y": 0.83577457205225891, "x": 7.2727272727272725}, {"y": 0.8868821020290788, "x": 7.3737373737373737}, {"y": 0.92894842923125132, "x": 7.4747474747474749}, {"y": 0.96154471402682351, "x": 7.5757575757575752}, {"y": 0.98433865788382358, "x": 7.6767676767676765}, {"y": 0.9970978909438748, "x": 7.7777777777777777}, {"y": 0.9996923408861117, "x": 7.8787878787878789}, {"y": 0.99209555893232282, "x": 7.9797979797979792}, {"y": 0.97438498947553576, "x": 8.0808080808080813}, {"y": 0.94674118058335432, "x": 8.1818181818181817}, {"y": 0.90944594342446261, "x": 8.282828282828282}, {"y": 0.86287947938178355, "x": 8.3838383838383841}, {"y": 0.80751650413956255, "x": 8.4848484848484844}, {"y": 0.74392140825684439, "x": 8.5858585858585865}, {"y": 0.67274250356226473, "x": 8.6868686868686869}, {"y": 0.59470541402449739, "x": 8.7878787878787872}, {"y": 0.51060567847428295, "x": 8.8888888888888893}, {"y": 0.42130064058860689, "x": 8.9898989898989896}, {"y": 0.32770070881349983, "x": 9.0909090909090899}, {"y": 0.23076007532505177, "x": 9.191919191919192}, {"y": 0.13146698864295842, "x": 9.2929292929292924}, {"y": 0.03083367906114098, "x": 9.3939393939393945}, {"y": -0.070113960400646769, "x": 9.4949494949494948}, {"y": -0.1703468323280965, "x": 9.5959595959595951}, {"y": -0.26884312591038401, "x": 9.6969696969696972}, {"y": -0.36459873365588863, "x": 9.7979797979797976}, {"y": -0.45663748763377376, "x": 9.8989898989898997}, {"y": -0.54402111088936989, "x": 10.0}], "name": "table001"}, {"values": [{"y": 1.0, "x": 0.0}, {"y": 0.99490281585683027, "x": 0.10101010101010101}, {"y": 0.97966322599969979, "x": 0.20202020202020202}, {"y": 0.95443658842014489, "x": 0.30303030303030304}, {"y": 0.91948007275227761, "x": 0.40404040404040403}, {"y": 0.8751500385908233, "x": 0.50505050505050508}, {"y": 0.82189840263017, "x": 0.60606060606060608}, {"y": 0.76026803165915058, "x": 0.70707070707070707}, {"y": 0.69088720837706741, "x": 0.80808080808080807}, {"y": 0.6144632264484674, "x": 0.90909090909090906}, {"y": 0.53177518009103919, "x": 1.0101010101010102}, {"y": 0.44366602170222852, "x": 1.1111111111111112}, {"y": 0.35103396849205021, "x": 1.2121212121212122}, {"y": 0.25482334572604864, "x": 1.3131313131313131}, {"y": 0.15601495992575853, "x": 1.4141414141414141}, {"y": 0.055616100165806738, "x": 1.5151515151515151}, {"y": -0.045349730601885241, "x": 1.6161616161616161}, {"y": -0.14585324951413534, "x": 1.7171717171717171}, {"y": -0.24486988668507895, "x": 1.8181818181818181}, {"y": -0.34139023004892061, "x": 1.9191919191919191}, {"y": -0.43443031567828566, "x": 2.0202020202020203}, {"y": -0.52304165867487507, "x": 2.1212121212121211}, {"y": -0.60632092237383539, "x": 2.2222222222222223}, {"y": -0.68341912729040333, "x": 2.3232323232323231}, {"y": -0.75355030592944461, "x": 2.4242424242424243}, {"y": -0.81599951522755698, "x": 2.5252525252525251}, {"y": -0.87013012494596542, "x": 2.6262626262626263}, {"y": -0.91539030771363583, "x": 2.7272727272727271}, {"y": -0.95131866455872793, "x": 2.8282828282828283}, {"y": -0.9775489285796396, "x": 2.9292929292929291}, {"y": -0.99381369880469395, "x": 3.0303030303030303}, {"y": -0.99994716617612389, "x": 3.131313131313131}, {"y": -0.9958868038686729, "x": 3.2323232323232323}, {"y": -0.98167400471107902, "x": 3.3333333333333335}, {"y": -0.95745365921233472, "x": 3.4343434343434343}, {"y": -0.92347267849447645, "x": 3.5353535353535355}, {"y": -0.88007747718967322, "x": 3.6363636363636362}, {"y": -0.8277104419618857, "x": 3.7373737373737375}, {"y": -0.7669054216542901, "x": 3.8383838383838382}, {"y": -0.69828228503755996, "x": 3.9393939393939394}, {"y": -0.62254060163933012, "x": 4.0404040404040407}, {"y": -0.54045251007479034, "x": 4.141414141414141}, {"y": -0.45285484658127084, "x": 4.2424242424242422}, {"y": -0.36064061400144803, "x": 4.3434343434343434}, {"y": -0.26474987818348289, "x": 4.4444444444444446}, {"y": -0.16616018460355267, "x": 4.545454545454545}, {"y": -0.065876592907246775, "x": 4.6464646464646462}, {"y": 0.035078569038604845, "x": 4.7474747474747474}, {"y": 0.13567612713271912, "x": 4.8484848484848486}, {"y": 0.23489055281917826, "x": 4.9494949494949498}, {"y": 0.33171041770321597, "x": 5.0505050505050502}, {"y": 0.42514870442477243, "x": 5.1515151515151514}, {"y": 0.51425286867696263, "x": 5.2525252525252526}, {"y": 0.59811454979355327, "x": 5.3535353535353538}, {"y": 0.67587883091212952, "x": 5.4545454545454541}, {"y": 0.74675295431144773, "x": 5.5555555555555554}, {"y": 0.81001440307560302, "x": 5.6565656565656566}, {"y": 0.86501826669756599, "x": 5.7575757575757578}, {"y": 0.91120381553440255, "x": 5.8585858585858581}, {"y": 0.94810021709176406, "x": 5.9595959595959593}, {"y": 0.97533133586373366, "x": 6.0606060606060606}, {"y": 0.99261956779670091, "x": 6.1616161616161618}, {"y": 0.99978867028732132, "x": 6.2626262626262621}, {"y": 0.99676555886452312, "x": 6.3636363636363633}, {"y": 0.98358105223952097, "x": 6.4646464646464645}, {"y": 0.96036955812852376, "x": 6.5656565656565657}, {"y": 0.92736770305097527, "x": 6.666666666666667}, {"y": 0.88491192007166875, "x": 6.7676767676767673}, {"y": 0.83343501907817941, "x": 6.8686868686868685}, {"y": 0.77346177455747467, "x": 6.9696969696969697}, {"y": 0.70560357585152533, "x": 7.0707070707070709}, {"y": 0.63055219442918808, "x": 7.1717171717171713}, {"y": 0.54907273171307958, "x": 7.2727272727272725}, {"y": 0.46199581935390133, "x": 7.3737373737373737}, {"y": 0.37020915146548028, "x": 7.4747474747474749}, {"y": 0.27464843514404769, "x": 7.5757575757575752}, {"y": 0.17628785152548898, "x": 7.6767676767676765}, {"y": 0.076130124624071926, "x": 7.7777777777777777}, {"y": -0.024803700805447838, "x": 7.8787878787878789}, {"y": -0.12548466817409182, "x": 7.9797979797979792}, {"y": -0.22488639862108173, "x": 8.0808080808080813}, {"y": -0.32199555429793802, "x": 8.1818181818181817}, {"y": -0.41582216870771721, "x": 8.282828282828282}, {"y": -0.50540973878806716, "x": 8.3838383838383841}, {"y": -0.58984497585570728, "x": 8.4848484848484844}, {"y": -0.66826711600762867, "x": 8.5858585858585865}, {"y": -0.73987669506531706, "x": 8.6868686868686869}, {"y": -0.80394369860703008, "x": 8.7878787878787872}, {"y": -0.85981500400366195, "x": 8.8888888888888893}, {"y": -0.90692103859135909, "x": 8.9898989898989896}, {"y": -0.94478158610502661, "x": 9.0909090909090899}, {"y": -0.97301068217978803, "x": 9.191919191919192}, {"y": -0.99132054901386579, "x": 9.2929292929292924}, {"y": -0.99952452908148015, "x": 9.3939393939393945}, {"y": -0.99753898798840768, "x": 9.4949494949494948}, {"y": -0.9853841670717991, "x": 9.5959595959595951}, {"y": -0.96318397705253245, "x": 9.6969696969696972}, {"y": -0.93116473484369155, "x": 9.7979797979797976}, {"y": -0.88965285639260161, "x": 9.8989898989898997}, {"y": -0.83907152907645244, "x": 10.0}], "name": "table002"}, {"values": [{"y": 0.0, "x": 0.0}, {"y": 0.48385164043793466, "x": 0.50505050505050508}, {"y": 0.84688556360298339, "x": 1.0101010101010102}, {"y": 0.99845222690038948, "x": 1.5151515151515151}, {"y": 0.90070544620295545, "x": 2.0202020202020203}, {"y": 0.5780525851065732, "x": 2.5252525252525251}, {"y": 0.11106003812412972, "x": 3.0303030303030303}, {"y": -0.38366419180611233, "x": 3.5353535353535355}, {"y": -0.78258750265420218, "x": 4.0404040404040407}, {"y": -0.98609877449092964, "x": 4.545454545454545}, {"y": -0.94338125844599963, "x": 5.0505050505050502}, {"y": -0.66510151497882242, "x": 5.5555555555555554}, {"y": -0.22074597455506337, "x": 6.0606060606060606}, {"y": 0.27872981867755731, "x": 6.5656565656565657}, {"y": 0.70860679769921819, "x": 7.0707070707070709}, {"y": 0.96154471402682351, "x": 7.5757575757575752}, {"y": 0.97438498947553576, "x": 8.0808080808080813}, {"y": 0.74392140825684439, "x": 8.5858585858585865}, {"y": 0.32770070881349983, "x": 9.0909090909090899}, {"y": -0.1703468323280965, "x": 9.5959595959595951}], "name": "table003"}, {"values": [{"y": 1.0, "x": 0.0}, {"y": 0.8751500385908233, "x": 0.50505050505050508}, {"y": 0.53177518009103919, "x": 1.0101010101010102}, {"y": 0.055616100165806738, "x": 1.5151515151515151}, {"y": -0.43443031567828566, "x": 2.0202020202020203}, {"y": -0.81599951522755698, "x": 2.5252525252525251}, {"y": -0.99381369880469395, "x": 3.0303030303030303}, {"y": -0.92347267849447645, "x": 3.5353535353535355}, {"y": -0.62254060163933012, "x": 4.0404040404040407}, {"y": -0.16616018460355267, "x": 4.545454545454545}, {"y": 0.33171041770321597, "x": 5.0505050505050502}, {"y": 0.74675295431144773, "x": 5.5555555555555554}, {"y": 0.97533133586373366, "x": 6.0606060606060606}, {"y": 0.96036955812852376, "x": 6.5656565656565657}, {"y": 0.70560357585152533, "x": 7.0707070707070709}, {"y": 0.27464843514404769, "x": 7.5757575757575752}, {"y": -0.22488639862108173, "x": 8.0808080808080813}, {"y": -0.66826711600762867, "x": 8.5858585858585865}, {"y": -0.94478158610502661, "x": 9.0909090909090899}, {"y": -0.9853841670717991, "x": 9.5959595959595951}], "name": "table004"}], "height": 320}, function(chart) {
chart({el: "#vis41930"}).update();
});
};
_do_plot();
})();
</script>
In [ ]:
Content source: aflaxman/mplexporter
Similar notebooks: