GeoPandas: Learn How to make Animated & highly-customizable Choropleth maps in Python.
If you want to learn how to construct a beautiful and highly customizable map in Python, this article will go over the many ways for wrangling and visualising geographical data in Python. Furthermore, if you’ve already started using Matplotlib and Pandas for data visualisation and are looking for the next easy step to begin working with geographical data. In fact, I spent hours researching several software packages like as Power Bi and Tableau to find the most basic yet extremely customizable solution for creating maps (particularly choropleths).
So, what exactly is Geopandas?
An open-source project called GeoPandas aims to simplify the handling of geographical data in Python. It combines the powers of Shapely with Pandas by offering a high-level interface to many geometries in Shapely and geospatial operations in Pandas. Users can now do operations in Python that would typically need a spatial database like PostGIS thanks to GeoPandas. It has the ability to read files as data frames known as Geodataframes, which are comparable to Pandas dataframes.
OK, let’s get started. Here is what you require. The goal is to build an annual carbon emission map of the various local authorities in the United Kingdom from 2009 to 2019. To keep track of all the code, I am using Jupyter Notebook, but you can use whatever works best for you.
Let’s add certain data in our notebook. I’ll be creating a map of the United Kingdom by counties (states), as I’m currently based in the United Kingdom. The website for the department of national statistics does an excellent job of making a lot of data available and public, and I came across this page with a variety of shape files with a variety of information.
The shapefile, however, is just one data layer. This will make it easier to construct the map, but we’ll need another dataset if we want to connect data to it. Let’s return to the UK government’s environment website and download the UK carbon emissions dataset as a .csv file. This .csv file includes multiple distinct columns related to UK emissions that we may utilise as variables for visualisation.
You can download shape-file and .csv from following links-
Loading Data- I’m back in my Jupyter Notebook since both datasets are downloaded and ready to use. It’s time to open the .csv and .shape file. I’m loading all necessary libraries for data analysis and map visualisation, such as pandas as pd and GeoPandas as gpd.
Data Cleaning- Data cleaning is an important part before visualisation. As you see there are total 379 rows in dataframe df with no null values. I’m retaining year columns Local Authority, Code,2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019. In addition, carbon emissions were computed using the total of road and village transit. And also, I’m dropping unnecessary columns from data frame df.
Let’s check the shape file. In the shape file we find the information about longitude and latitude along with polygon and geometry columns. These important parameters for mapping.
Using the shape file, let’s print the map quickly.
Merging Datasets- Now, the next step is to load a data CSV file to join with the shape file. The polygons in the carbon emission dataset for each local authority code must match up with the shape file, which is considered an important attribute for integrating the two data sources. In the Python environment, I am using merge function to combine the shape file to the CSV emission data file. Combining both datasets on the basis of local authority codes and I set the how for left join.
For more practical mapping, I’m creating a new data frame with columns representing years and rows representing carbon emission amounts connected with local authority codes.
Plotting Map- Now, the next step is to load a data CSV file to join with the shape file. The polygons in the carbon emission dataset for each local authority code must match up with the shape file, which is considered an important attribute for integrating the two data sources. In the Python environment, I am using merge function to combine the shape file to the CSV emission data file. For more practical mapping, I’m creating a new data frame with columns representing years and rows representing carbon emission amounts connected with local authority codes. Plotting a map using a merged new data frame. There are several aspects to consider here-
a) Column- Indicates which column in the data frame is being utilized to represent the attribute on the map.
b) cmap- This study, is employed to define the color spectrum for the emissions map. ‘RdPu’ was chosen for this study. Matplotlib includes a variety of color schemes from which to pick.
c) edgecolour- The color of the border between the states can range from [0,1], with 1 being white and 0 representing black.
d) legend- A diagram that explains the meaning of each color in the colour spectrum, which serves as the map’s visual style.
e) Fig-Size- Size of fig according to X and Y-axis.
And Here we go! Tadaaaaa! We did it!
But, What if we need to create a map for each year and save it in local directory?
Firstly, I’m specifying a local directory to save all images generated by the program/function. Setting the legend minimum value (vmin)-200 and maximum value (vmax)-1600.
Secondly, I'm creating a loop to construct a map for each year (2009-2019). I also want a "Year" annotation for each image, so I'm implementing another function loop to do this. Points to consider-
a) only_year= year[:4]- Taking only 4 values for title.
b) fig.annotate- Function for annotation which explains alignments, font size, XY-axis position.
Finally, I created a function that saves all generated year wise map images with the title (only_year+’_emissions.jpg’) and will automatically increase using a loop.
You may also change the title, fonts, size, and other aspects of the visualisation (please check the matplotlib documentation for more information). I also used ImageMagick for GIF creation**
I hope you found this essay useful. Thank you for your time and support. Please feel free to contact me with any questions or suggestions . Best wishes! You can add me on LinkedIn https://www.linkedin.com/in/umesh-kumawat/