{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/folium/__init__.py\n", "0.2.1\n" ] } ], "source": [ "import folium\n", "\n", "print(folium.__file__)\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make a figure" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import numpy.ma as ma\n", "\n", "\n", "def make_data():\n", " x = np.linspace(-np.pi, np.pi, 101)\n", " sin = np.sin(x)\n", " cos = np.cos(x)\n", " cos[20:50] = np.NaN\n", " return pd.DataFrame(np.asanyarray([sin, cos]).T, columns=['sin', 'cos'], index=x)\n", "\n", "df = make_data()\n", "resolution, width, height = 75, 7, 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Instantiate the map" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "station = '42'\n", "lon, lat = -42, -21\n", "mapa = folium.Map(location=[lat, lon], zoom_start=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PNG" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import base64\n", "import matplotlib.pyplot as plt\n", "\n", "fig, ax = plt.subplots(figsize=(width, height))\n", "ax = df.plot(ax=ax, legend=False)\n", "ax.set_ylabel('Sea surface height (m)')\n", "png = 'mpld3_{}.png'.format(station)\n", "fig.savefig(png, dpi=resolution)\n", "\n", "encoded = base64.b64encode(open(png, 'rb').read())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from folium.element import IFrame\n", "\n", "html = ''.format\n", "iframe = IFrame(html(encoded), width=(width*resolution)+20, height=(height*resolution)+20)\n", "popup = folium.Popup(iframe, max_width=2650)\n", "\n", "icon = folium.Icon(color=\"red\", icon=\"ok\")\n", "marker = folium.Marker(location=[lat-2, lon-1], popup=popup, icon=icon)\n", "mapa.add_children(marker);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega/Vincent" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import json\n", "import vincent\n", "\n", "df.fillna(value='null', inplace=True) # Does not handle missing values.\n", "vis = vincent.Line(df, width=width*resolution, height=height*resolution)\n", "vis.legend(title='Vega');" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vega = folium.Vega(json.loads(vis.to_json()), width=\"100%\", height=\"100%\")\n", "popup = folium.Popup(max_width=vis.width+75).add_child(vega)\n", "\n", "icon = folium.Icon(color=\"green\", icon=\"ok\")\n", "marker = folium.Marker(location=[lat-1, lon+1], popup=popup, icon=icon)\n", "\n", "mapa.add_children(marker);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### mpld3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import mpld3\n", "from folium.element import IFrame\n", "from mpld3.plugins import LineLabelTooltip, connect\n", "\n", "\n", "fig, ax = plt.subplots(figsize=(width, height))\n", "ax = df.plot(ax=ax, legend=False)\n", "ax.set_ylabel('Sea surface height (m)')\n", "\n", "[mpld3.plugins.connect(fig, LineLabelTooltip(line, name)) for\n", " line, name in zip(ax.lines, df.columns)]\n", "\n", "figname = 'mpld3_{}.html'.format(station)\n", "mpld3.save_html(fig, figname)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open(figname, 'r') as f:\n", " html = f.read()\n", "\n", "iframe = IFrame(html, width=(width*resolution)+75, height=(height*resolution)+50)\n", "popup = folium.Popup(iframe, max_width=2650)\n", "\n", "icon = folium.Icon(color=\"blue\", icon=\"ok\")\n", "marker = folium.Marker(location=[lat+1, lon-1], popup=popup, icon=icon)\n", "\n", "mapa.add_children(marker);" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:92: DeprecationWarning: DisplayFormatter._ipython_display_formatter_default is deprecated: use @default decorator instead.\n", " def _ipython_display_formatter_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:98: DeprecationWarning: DisplayFormatter._formatters_default is deprecated: use @default decorator instead.\n", " def _formatters_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:677: DeprecationWarning: PlainTextFormatter._deferred_printers_default is deprecated: use @default decorator instead.\n", " def _deferred_printers_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:669: DeprecationWarning: PlainTextFormatter._singleton_printers_default is deprecated: use @default decorator instead.\n", " def _singleton_printers_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:672: DeprecationWarning: PlainTextFormatter._type_printers_default is deprecated: use @default decorator instead.\n", " def _type_printers_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:669: DeprecationWarning: PlainTextFormatter._singleton_printers_default is deprecated: use @default decorator instead.\n", " def _singleton_printers_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:672: DeprecationWarning: PlainTextFormatter._type_printers_default is deprecated: use @default decorator instead.\n", " def _type_printers_default(self):\n", "/home/filipe/.virtualenvs/iris/lib/python2.7/site-packages/IPython/core/formatters.py:677: DeprecationWarning: PlainTextFormatter._deferred_printers_default is deprecated: use @default decorator instead.\n", " def _deferred_printers_default(self):\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapa" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TODO: Bokeh" ] } ], "metadata": { "kernelspec": { "display_name": "Iris (Python 2)", "language": "python", "name": "iris_python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.8" } }, "nbformat": 4, "nbformat_minor": 0 }