Remember: in the previous tutorial, we calculated the average sunshine for each of the French regions and represented the results in a histogram. This presentation can certainly be improved... In this tutorial, we'll look at how to display this data on an interactive map in html format.
Here's what the final result will look like:
You can access this map and test it yourself by clicking here.
Folium cardmaking: the basics
Folium is a Python library that makes it easy to create professional-looking interactive maps, comparable to those you might create with a pay-per-use mapping tool or find in some media.
For this tutorial, you'll need to install Folium. You'll also need Geopandas.
Are you all set? Let's start by creating a blank map centered on France. It couldn't be simpler:
You get a map like this one, on which you can zoom in and out and move around freely:
How do I display data on this map? For a simple example, let's start by adding a marker :
The marker now appears on your map:
In the same way, you can add just about anything to this map: points, shapes, images, graphics, html... or all of the above!
Draw a shapefile on the map
In our case, what are we going to represent on our map? We want to draw the different regions of France and color each of them with a color corresponding to the average amount of sunshine it receives. This is what we call a chloropleth map.
To understand the principle, let's start by drawing a single region without worrying about its color.
The first steps are the same as in the previous tutorial:
Download region shapes in shapefile format (e.g. here),
Open this file with Geopandas,
Filter to keep only metropolitan regions,
Select the region to be displayed on the map (for a change, we'll use Normandy).
We don't need all the information contained in the geodataframe. We'll keep only the geometry column (which contains the region's shape) and the region name, which we'll index. Next, we'll convert the geodataframe into GeoJSON.
Sounds complicated? It takes less time to do than to explain:
All that remains now is to create a blank map, then add a chloropleth containing only our region:
The result is as follows:
To draw all the regions, simply repeat these steps, omitting only the Normandy region:
Creating a chloropleth map with Folium and geopandas
Our aim is not only to draw the regions, but also to color them according to their average insolation.
To do this, let's take the sunshine data calculated in a previous tutorial. If you didn't follow it, you can download the result here.
First of all, we'll open these data with Pandas, then convert them: sunshine was evaluated in Joules per square metre per day, but kilowatt-hours per square metre will be more meaningful. Finally, we'll round these values off to the first decimal place.
From this data, let's add a new column containing a small string of characters that can be used as a legend. For example: “Corsica: 4.5kWh/m²”.
Next, we'll merge this dataframe with the geodataframe containing regional shapes.
We can now create a new chloropleth with these data:
Once again, all that's left to do is create a blank map and add this chloropleth:
The last line saves the result as an html file. This format makes it easy to share the map on the Internet. You can access it here, but it could also be integrated into a web page, etc.
About: Callendar is a start-up specializing in the development of innovative solutions for climate risk assessment. Aware of the challenge of adapting to climate change, we strive to share our expertise through free tools and tutorials like this one.