{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8.3+52.g2758dc7.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
languagecoordinatesconsonantsvowels
0Turkish(39.8667, 32.8667)258
1Korean(37.5, 128.0)2111
2Tiwi(-11.6308, 130.94899999999998)224
3Liberia Kpelle(6.92048, -9.96128)2212
4Tulu(12.8114, 75.2651)2413
\n", "
" ], "text/plain": [ " language coordinates consonants vowels\n", "0 Turkish (39.8667, 32.8667) 25 8\n", "1 Korean (37.5, 128.0) 21 11\n", "2 Tiwi (-11.6308, 130.94899999999998) 22 4\n", "3 Liberia Kpelle (6.92048, -9.96128) 22 12\n", "4 Tulu (12.8114, 75.2651) 24 13" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas\n", "import ast\n", "\n", "\n", "data = pandas.read_csv(\n", " 'data' + os.path.sep + 'consonants_vowels.csv',\n", " # To ensure that tuples are read as tuples\n", " converters={'coordinates': ast.literal_eval}\n", ")\n", "\n", "data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Charts" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import io\n", "\n", "\n", "pie_charts_data = zip(data.consonants, data.vowels)\n", "\n", "fig = plt.figure(figsize=(0.5, 0.5))\n", "fig.patch.set_alpha(0)\n", "ax = fig.add_subplot(111)\n", "plots = []\n", "for sizes in pie_charts_data:\n", " ax.pie(sizes, colors=('#e6194b', '#19e6b4'))\n", " buff = io.StringIO()\n", " plt.savefig(buff, format='SVG')\n", " buff.seek(0)\n", " svg = buff.read()\n", " svg = svg.replace('\\n', '')\n", " plots.append(svg)\n", " plt.cla()\n", "plt.clf()\n", "plt.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Legend" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import branca\n", "\n", "\n", "legend_html = '''\n", "{% macro html(this, kwargs) %}\n", "
\n", "

 Consonants

\n", "

 Vowels

\n", "
\n", "
\n", "
\n", "{% endmacro %}\n", "'''\n", "\n", "legend = branca.element.MacroElement()\n", "legend._template = branca.element.Template(legend_html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=(0,0), zoom_start=2)\n", "\n", "for i, coord in enumerate(data.coordinates):\n", " marker = folium.Marker(coord)\n", " icon = folium.DivIcon(html=plots[i])\n", " marker.add_child(icon)\n", " popup = folium.Popup(\n", " 'Consonants: {}
\\nVowels: {}'.format(data.consonants[i], data.vowels[i])\n", " )\n", " marker.add_child(popup)\n", " m.add_child(marker)\n", "m.get_root().add_child(legend)\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }