From 07966603ace5074a9c0287233ee4e68a8da6d2bf Mon Sep 17 00:00:00 2001 From: OneAdder Date: Tue, 14 May 2019 12:45:38 +0300 Subject: [PATCH] minicharts example using matplotlib --- examples/Minicharts.ipynb | 269 ++++++++++++++++++++++++++++ examples/data/consonants_vowels.csv | 19 ++ 2 files changed, 288 insertions(+) create mode 100644 examples/Minicharts.ipynb create mode 100644 examples/data/consonants_vowels.csv diff --git a/examples/Minicharts.ipynb b/examples/Minicharts.ipynb new file mode 100644 index 000000000..f379a5420 --- /dev/null +++ b/examples/Minicharts.ipynb @@ -0,0 +1,269 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "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": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "import pandas\n", + "import ast\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", + "data.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Charts" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import io\n", + "\n", + "pie_charts_data = zip(data.consonants, data.vowels)\n", + "\n", + "#Note that figsize should be small\n", + "fig = plt.figure(figsize=(0.5, 0.5))\n", + "#We need to make everything except for axes invisible\n", + "fig.patch.set_alpha(0)\n", + "#Add a subplot\n", + "ax = fig.add_subplot(111)\n", + "#We are going to store plots as SVG in this list\n", + "plots = []\n", + "#Now we need to walk in our data\n", + "for sizes in pie_charts_data:\n", + " #Create the pie chart\n", + " ax.pie(sizes, colors=('#e6194b', '#19e6b4'))\n", + " #There is no normal way to get it as str\n", + " buff = io.StringIO()\n", + " plt.savefig(buff, format='SVG')\n", + " buff.seek(0)\n", + " svg = buff.read()\n", + " #Sometimes it doesn't wors with \\n's\n", + " svg = svg.replace('\\n', '')\n", + " #Append to our list\n", + " plots.append(svg)\n", + " #Now we need to flush plt, it's important\n", + " plt.cla()\n", + "#Close the plot\n", + "plt.clf()\n", + "plt.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Legend" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import branca\n", + "\n", + "#Let's make a legend using branca\n", + "legend_html = '''\n", + "{% macro html(this, kwargs) %}\n", + "
\n", + "

 Consonants

\n", + "

 Vowels

\n", + "
\n", + "
\n", + "
\n", + "{% endmacro %}\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": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import folium\n", + "\n", + "#Finally, we can draw it\n", + "m = folium.Map(location=(0,0), zoom_start=2)\n", + "#print(data.coordinates)\n", + "for i, coord in enumerate(data.coordinates):\n", + " marker = folium.Marker(coord)\n", + " #The actual magic is happening right here\n", + " #We pass SVG strings as html\n", + " icon = folium.DivIcon(html=plots[i])\n", + " marker.add_child(icon)\n", + " #Let's add raw data into popups\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", + "#And finally the legend\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.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/data/consonants_vowels.csv b/examples/data/consonants_vowels.csv new file mode 100644 index 000000000..b31ec2d56 --- /dev/null +++ b/examples/data/consonants_vowels.csv @@ -0,0 +1,19 @@ +language,coordinates,consonants,vowels +Turkish,"(39.8667, 32.8667)",25,8 +Korean,"(37.5, 128.0)",21,11 +Tiwi,"(-11.6308, 130.94899999999998)",22,4 +Liberia Kpelle,"(6.92048, -9.96128)",22,12 +Tulu,"(12.8114, 75.2651)",24,13 +Mapudungun,"(-38.7392, -71.277)",20,6 +Kiowa,"(34.9403, -98.9042)",22,20 +Japanese,"(35.0, 135.0)",15,5 +Yoruba,"(7.15345, 3.67225)",18,11 +Finnish,"(64.7628, 25.5577)",17,8 +Hawaiian,"(19.6297, -155.43)",8,5 +Hungarian,"(46.9068585714, 19.6555271429)",26,14 +Georgian,"(41.850396999999994, 43.78613)",28,6 +Amharic,"(11.708182, 39.543456)",30,7 +Sandawe,"(-5.26918, 35.4808)",47,7 +Tlingit,"(59.4447, -135.29)",43,5 +Lakota,"(46.3699, -103.95)",28,8 +Yucatec Maya,"(18.7757, -88.9567)",20,10