#!/usr/bin/env python # coding: utf-8 # # Pie chart on map # 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"](https://en.wikipedia.org/wiki/2006_Montenegrin_independence_referendum). # In[1]: import pandas as pd from lets_plot import * from lets_plot.mapping import * # In[2]: LetsPlot.setup_html() # In[3]: referendum_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/montenegrin_referendum_2006.csv") referendum_df.head() # 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. # In[4]: from lets_plot.geo_data import * country = 'Montenegro' municipalities = geocode_states(names=referendum_df['Municipality']).scope(country) boundaries = municipalities.get_boundaries(resolution=15) # In[5]: boundaries = pd.merge(boundaries, referendum_df[['Municipality','Yes%']], left_on='state', right_on='Municipality') boundaries.head() # In[6]: 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 + ggsize(800, 700) + theme_void() # Let's prepare the data for visualization and add more information. # The table will contain the following columns: # - 'Municipality' - the name of the municipality # - 'Registered' - registered voters # - 'Vote' - choice: # - "Yes" - for the independence option # - "No" - against independence # - "Blank" - invalid or blank votes # - 'Number' - number of 'Vote' # In[7]: mref_df = referendum_df # Blank or invalid votes: mref_df["Blank"] = mref_df["Voted"] - mref_df["Yes"] - mref_df["No"] mref_df = mref_df[["Municipality", "Registered", "No", "Yes", "Blank"]] id_vars = ["Municipality", "Registered"] mref_df = pd.melt(frame=mref_df, id_vars=id_vars, var_name="Vote", value_name="Number") mref_df # Display the voting results as pie charts. Place them in the center of the corresponding municipalities by connecting `data` and `map` with the parameter `map_join`. # In[8]: ggplot() + \ map_layer + \ geom_pie(aes(paint_a='Vote', slice='Number'), data=mref_df, stat='identity', map=municipalities, map_join=['Municipality','state'], fill_by='paint_a', size=5, stroke=1.5) + \ scale_manual('paint_a', values=["#e41a1c","#4daf4a", "#999999"]) + \ plot_title + \ ggsize(800, 700) + \ theme_void() # In the 'count2d' statistical transformation, we will use the calculated variables: the percentage of votes ('..prop..') and the total number of voters in the municipality ('..sum..'). We will show the details in the tooltips. And let's make the pie area corresponds to the number of voters. # In[9]: pie_layer = geom_pie(aes('Municipality', paint_a='Vote', weight='Number', size='..sum..', group='Municipality'), data=mref_df, map=municipalities, map_join=['Municipality','state'], fill_by='paint_a', hole=0.2, stroke=1.5, stroke_side='both', tooltips=layer_tooltips() .title('@Municipality') .line('Vote|@Vote') .line('Number|@{..count..}') .line('Percent|@{..prop..}') .line('Total voted|@{..sum..}') .line('Registered|@Registered') .format('..prop..', '.2%') .format('Registered', ',d')) + \ scale_manual('paint_a', values=['#e41a1c','#4daf4a', '#999999']) + \ scale_size(name='Total voted', range=[3, 8]) ggplot() + \ map_layer + \ pie_layer + \ plot_title + \ plot_title + \ ggsize(800, 700) + \ theme_void() # Add livemap. # In[10]: ggplot() + \ geom_livemap(data_size_zoomin=3) + \ map_layer + \ pie_layer + \ plot_title + \ theme(legend_position='none')