How to draw a variety of maps with folium in python?
This blog talks about how to draw a map with python module "folium", like how to display all locations with points or with cluster, how to paint areas with different colors, how to add labels or po...
A map can clearly present information in terms of geography. Recently I learnt
how to realize geovisualization with folium module in Python. In this blog, I
will talk about how to draw a different types of map like with the following
points:
Datasets
Display all locations with points on the map
Display all locations with clusters
Paint areas with different colors
Add labels on the map
Add polygon borders
Show changes in terms of timing with heatmap
Show changes in terms of timing with choropleth
Datasets
Before drawing maps, I’ll talk about the datasets which are used for this blog.
I downloaded Airbnb Paris’ datasets “listings” and
“neighbourhoods”.
In order to put points on the map, we need to convert each coordinate to
geopoint, same for the dataframe.
Display all locations with points on the map
There are numerous marker types, I apply CircleMarker with a popup(input
text or visualization for object displayed when clicking) and tooltip(display
a text when hovering over the object), apply LayerControl to filter different
types of locations.
Display all locations with clusters
Before drawing CircleMarker on the map, I add plugins.MarkerCluster() on
the map for clustering coordinates.
Paint areas with different colors
To paint areas in terms of locations’ average price, we need to calculate the
values firstly. Then use branca.colormap.linear to set colormap, insert the
colormap into style_function, plot a GeoJSON overlay on the base map with
folium.GeoJson, then we can draw the map.
Add labels on the map
Do you notice that? The labels are usually covered by painted colors. In this
case, I used folium.TileLayer to add CartoDBPositronOnlyLabels on the map,
so that we add another labels’ layer.
Add polygon borders
I downloaded Ile-de-France’s geojson data from here. To draw
polygons’ border, the only thing that we need to do is add the polygons’
coordinates and set fillOpacity to be 0.
Show changes in terms of timing with heatmap
For presenting the changes in terms of time on a heatmap, we can apply
plugins.HeatMapWithTime(). data is the points you want to plot, which is a
list of list of points of the form [lat, lng] or [lat, lng, weight]. index
gives the label (or timestamp) of the elements of data, should be the same
length as data, or is replaced by a simple count if not specified.
Show changes in terms of timing with choropleth
The map above describes the price’s changes of each neighbourhood in terms of
time series. Thus, before making the map, we need to calculate the average
location’s price for each neighbourhood each year.
Then I prepared convert datetime to U10 with pandas.DatetimeIndex() and
astype().
The last step is to define a color map in terms of neighbourhoods’ prices for
each year.
We can apply TimeSliderChoropleth to draw the time series map. data should
be geojson string, styledict is the dictionary where the keys are the geojson
feature ids and the values are dicts of {time: style_options_dict}.