Let's draw pie chart on map.
Take as an example the results of the Montenegrin independence referendum in 2006. The dataset was downloaded from Wikipedia article "2006 Montenegrin independence referendum".
from lets_plot import *
from lets_plot.mapping import *
LetsPlot.setup_html()
import pandas as pd
referendum_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/montenegrin_referendum_2006.csv")
referendum_df.head()
Municipality | Yes | Yes% | No | No% | Registered | Voted | Voted% | |
---|---|---|---|---|---|---|---|---|
0 | Andrijevica | 1084 | 27.60 | 2824 | 71.89 | 4369 | 3928 | 89.91 |
1 | Bar | 16640 | 63.07 | 9496 | 35.99 | 32255 | 26382 | 81.79 |
2 | Berane | 11268 | 46.85 | 12618 | 52.46 | 28342 | 24051 | 84.86 |
3 | Bijelo Polje | 19405 | 55.36 | 15437 | 44.04 | 40110 | 35051 | 87.39 |
4 | Budva | 5908 | 52.75 | 5180 | 46.25 | 12797 | 11200 | 87.52 |
Let's find geographical boundaries of these states using Lets-Plot
geocoding module. And draw the percentage of those who voted "for" on the map by regions.
from lets_plot.geo_data import *
country = 'Montenegro'
municipalities = geocode_states(names=referendum_df['Municipality']).scope(country)
boundaries = municipalities.get_boundaries(resolution=15)
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).
boundaries = pd.merge(boundaries, referendum_df[['Municipality','Yes%']], left_on='state', right_on='Municipality')
boundaries.head()
state | found name | geometry | Municipality | Yes% | |
---|---|---|---|---|---|
0 | Andrijevica | Opština Andrijevica | MULTIPOLYGON (((19.61901 42.80456, 19.63347 42... | Andrijevica | 27.60 |
1 | Bar | Opština Bar | MULTIPOLYGON (((18.98948 42.16385, 18.98910 42... | Bar | 63.07 |
2 | Berane | Opština Berane | MULTIPOLYGON (((19.61729 42.88021, 19.62012 42... | Berane | 46.85 |
3 | Bijelo Polje | Opština Bijelo Polje | MULTIPOLYGON (((19.41061 43.07992, 19.41992 43... | Bijelo Polje | 55.36 |
4 | Budva | Opština Budva | MULTIPOLYGON (((18.80014 42.28039, 18.79997 42... | Budva | 52.75 |
blank_theme = theme(line=element_blank(), axis=element_blank())
plot_title = ggtitle("Results of the Montenegrin independence referendum, 2006")
fill_colors = ['#a50026', '#d73027', '#66bd63', '#4daf4a','#006837']
map_layer = geom_map(aes(fill="Yes%"),
data=boundaries,
tooltips=layer_tooltips()
.title('@state')
.line('\'Yes\' votes|@{Yes%}')
.format('Yes%', '{} %')) + \
scale_fill_gradientn(fill_colors)
ggplot() + map_layer + plot_title + blank_theme + ggsize(800, 700)