Creating web maps from Python

In my work flow I generally use JavaScript to do graphs and maps. That’s because most of my work lives online. But before I get to map or graph I will have to do quite a bit of work in python. Which means a lot of work in python and then a bit of work in JS. I always wondered if I can bring them together. One way was to do everything in JS. But I do quite a bit of cleaning, processing and analyzing than actually visualizing. Python is probably the best tool for that. So my search for doing graphs and maps in Python began.

In this post I will write Folium. Follium allows you to create maps using Leaflet from Python. Python code is simple and easily understandable. It integrates well with Pandas.

The above choropleth map is created using the python code below.

import folium
import numpy as np
import pandas as pd

#initialize a map, with center roughly the center of Bangalore
#I have used MG Road metro station, with default zoom of 10 
map_osm = folium.Map(location=[ 12.9756295,77.6044286 ], \
			zoom_start=11,width=900, height = 600)

#Read the csv into a dataframes
df = pd.read_csv('Bangalore_BBMP_2010_results.csv', dtype={'Ward_No': 'str'})

#first name is the name of the column which has the key to match in the map
#second one is the value
value_columns = ['Ward_No', 'Voter_turnout_calc'] 

map_osm.geo_json(geo_path='BBMP_Wards_2011_region.json', data_out='data.json', \
data=df, columns=value_columns, key_on='', \
fill_color='YlGnBu', legend_name='Voter Turnout in 2010')


As you can see code is simple and you can easily understand. Follium can do many other things. Go explore.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *