|
| 1 | +# Piechart icons |
| 2 | + |
| 3 | +In this example we show how you can make mini-charts on several locations. |
| 4 | +We'll make little piecharts showing the number of consonants and vowels in |
| 5 | +a couple of languages. Those piecharts will be included as icons on the map. |
| 6 | + |
| 7 | +```{code-cell} ipython3 |
| 8 | +import os |
| 9 | +import ast |
| 10 | +
|
| 11 | +import pandas |
| 12 | +
|
| 13 | +data = pandas.read_csv( |
| 14 | + "../data/consonants_vowels.csv", |
| 15 | + # To ensure that tuples are read as tuples |
| 16 | + converters={"coordinates": ast.literal_eval}, |
| 17 | +) |
| 18 | +
|
| 19 | +data.head() |
| 20 | +``` |
| 21 | + |
| 22 | +## Pie charts |
| 23 | + |
| 24 | +```{code-cell} ipython3 |
| 25 | +import io |
| 26 | +
|
| 27 | +import matplotlib.pyplot as plt |
| 28 | +
|
| 29 | +pie_charts_data = zip(data.consonants, data.vowels) |
| 30 | +
|
| 31 | +fig = plt.figure(figsize=(0.5, 0.5)) |
| 32 | +fig.patch.set_alpha(0) |
| 33 | +ax = fig.add_subplot(111) |
| 34 | +plots = [] |
| 35 | +for sizes in pie_charts_data: |
| 36 | + ax.pie(sizes, colors=("#e6194b", "#19e6b4")) |
| 37 | + buff = io.StringIO() |
| 38 | + plt.savefig(buff, format="SVG") |
| 39 | + buff.seek(0) |
| 40 | + svg = buff.read() |
| 41 | + svg = svg.replace("\n", "") |
| 42 | + plots.append(svg) |
| 43 | + plt.cla() |
| 44 | +plt.clf() |
| 45 | +plt.close() |
| 46 | +``` |
| 47 | + |
| 48 | +## Legend |
| 49 | + |
| 50 | +```{code-cell} ipython3 |
| 51 | +import branca |
| 52 | +
|
| 53 | +legend_html = """ |
| 54 | +{% macro html(this, kwargs) %} |
| 55 | +<div style=" |
| 56 | + position: fixed; |
| 57 | + bottom: 50px; |
| 58 | + left: 50px; |
| 59 | + width: 250px; |
| 60 | + height: 80px; |
| 61 | + z-index:9999; |
| 62 | + font-size:14px; |
| 63 | + "> |
| 64 | + <p><a style="color:#e6194b;font-size:150%;margin-left:20px;">◼</a> Consonants</p> |
| 65 | + <p><a style="color:#19e6b4;font-size:150%;margin-left:20px;">◼</a> Vowels</p> |
| 66 | +</div> |
| 67 | +<div style=" |
| 68 | + position: fixed; |
| 69 | + bottom: 50px; |
| 70 | + left: 50px; |
| 71 | + width: 150px; |
| 72 | + height: 80px; |
| 73 | + z-index:9998; |
| 74 | + font-size:14px; |
| 75 | + background-color: #ffffff; |
| 76 | + filter: blur(8px); |
| 77 | + -webkit-filter: blur(8px); |
| 78 | + opacity: 0.7; |
| 79 | + "> |
| 80 | +</div> |
| 81 | +{% endmacro %} |
| 82 | +""" |
| 83 | +
|
| 84 | +legend = branca.element.MacroElement() |
| 85 | +legend._template = branca.element.Template(legend_html) |
| 86 | +``` |
| 87 | + |
| 88 | +## Map |
| 89 | + |
| 90 | +```{code-cell} ipython3 |
| 91 | +import folium |
| 92 | +
|
| 93 | +
|
| 94 | +m = folium.Map(location=(0, 0), zoom_start=2) |
| 95 | +
|
| 96 | +for i, coord in enumerate(data.coordinates): |
| 97 | + marker = folium.Marker(coord) |
| 98 | + icon = folium.DivIcon(html=plots[i]) |
| 99 | + marker.add_child(icon) |
| 100 | + popup = folium.Popup( |
| 101 | + "Consonants: {}<br>\nVowels: {}".format(data.consonants[i], data.vowels[i]) |
| 102 | + ) |
| 103 | + marker.add_child(popup) |
| 104 | + m.add_child(marker) |
| 105 | +m.get_root().add_child(legend) |
| 106 | +m |
| 107 | +``` |
0 commit comments