Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Putting It All Together: World Ecological Footprint

00:00 In this summarization lesson, you’ll practice all you’ve learned about Folium by creating a choropleth map for the world based on ecological footprint data.

00:11 By the end of this lesson, you’ll roughly recreate this world map using Folium. This choropleth displays the ecological footprint of countries of the world, where the numerical values represent global hectares per capita, or gha.

00:27 For reference, our planet can recoup about 1.6 global hectares per person, so countries exceeding 1.6 gha operate at a resource demand level that’s eventually unsustainable.

00:43 In order to make this map in Folium, you’ll create a base map for the world and select your web tiles. You’ll update the starting location and zoom level for an appropriate view of the world. You’ll add boundaries for each country through GeoJSON data, and then you’ll add a choropleth layer based on ecological footprint data.

01:02 Finally, you’ll customize the map to make it look more like the original by updating the color scheme, binning, and so on. Let’s get started. Recall that you can make a base map in Folium with folium.Map().

01:18 Let’s save that as eco_map. With these default tiles, the ocean is shaded blue, which may distract from the data. Let’s switch over to the "CartoDB positron" tiles for a cleaner look.

01:34 And now zoom in a bit so that you can see just one view of the world instead of multiples. Let’s set the location to be 30, 10 and the zoom_start property to be 2. Great.

01:49 You now have a solid base map that you can build your choropleth on. Next, you’ll need a GeoJSON file that dictates the countries’ boundaries. This time, instead of linking up with a GeoJSON file from your computer, let’s use a URL that provides this data for you.

02:08 The Natural Earth Project provides the type of data that you’ll need through its geojson.xyz service. For this map, you can use the admin 0 countries resource since it offers relatively high-quality borders for political countries of the world.

02:28 Once you’ve saved that URL as the string political_countries_url, you can pass that Python variable directly to folium.GeoJson() and add those borders to your eco_map.

02:42 You won’t actually need to visualize these borders on your choropleth, but you will need to link up the same GeoJSON data.

02:52 So now that you have GeoJSON data with the boundaries you need, it’s time to make your initial choropleth layer.

03:00 You can find data on Wikipedia in this list of countries by ecological footprint.

03:09 And we’ve gone ahead and converted that data into a CSV called footprint.csv, which we’ve stored in the same directory as this Jupyter Notebook.

03:19 Use pandas to read that CSV and store its contents as a pandas DataFrame called eco_footprints. Here are the first five rows. Each row is a country or region with various ecological measurements.

03:36 The Country/region column has the name of each area, and the Ecological footprint column has the data you’ll want to display on your choropleth.

03:47 When adding this dataset to your map, you’ll need to link up the country names with some property in your GeoJSON. So let’s look at the GeoJSON data by visiting the political countries URL in our browser. There are several properties here for each country, including its postal code and an estimate for its population.

04:09 While there are several options, the "name" property seems to have the names that match with the ecological footprints data.

04:20 To add a choropleth layer, use folium.Choropleth, then supply the geodata, which will be the political countries URL in this case, as well as the data that you want to display. Here that’s the eco_footprints DataFrame.

04:37 The data columns of interest are the "Country/region", which will contain the country names, and the "Ecological footprint" column, which has the choropleth data. Finally, you want to match up the names in the "Country/region" column with the name property of your GeoJSON, so key_on equals "feature.properties.name".

05:04 and this choropleth layer should be added to your eco_map.

05:10 Excellent. You now have an ecological footprint choropleth for countries of the world. The map doesn’t quite look like the example you want to re-create, however, so let’s do some styling. For starters, you can update the color scheme.

05:25 The default blue color palette doesn’t quite match the urgency of the situation, so let’s switch to green at the lower end of the spectrum and red at the upper end.

05:36 Switch fill_color="RdYlGn_r", which represents a Red-Yellow-Green color palette, and the _r is for reverse ordering. You have a few regions with dark coloring.

05:53 These are areas that are either missing data points or are missing matching names. The dark colors tend to distract from the other countries that are filled in, so let’s also set nan_fill_color to be "white" so that those areas dominate less.

06:12 This is already looking much better, but you can also update the opacity to make the countries less transparent, while making the borders of each country more so. Update the property fill_opacity to be 0.8 and line_opacity to be 0.3.

06:33 One of the bigger updates that you likely already noticed involves the binning for each color. The example world map features custom cutpoints, with any country over 8 gha displayed in the darkest red color, so let’s make our own bins for this choropleth. To do so, you’ll need to know the minimum and maximum values.

06:55 Global hectare won’t be negative, so you can use 0 for the start of the smallest bin, and for the end of the upmost bin, just create a Python variable to represent the maximum of the ecological footprint column.

07:10 Then update the bins property in your choropleth with your desired cut points, starting from 0 and ending at the max_eco_footprint.

07:25 As a final customization, you can also title your color bar so that your users know what the values of your map represent. Let’s update legend_name to be "Ecological Footprint per Capita (gha)". Excellent.

07:42 Your Folium map looks very similar to the original figure you wanted to re-create, and this map is interactive.

07:52 You can save your map as an HTML, open it,

07:59 and zoom in to explore each region further.

08:05 You created a Folium map for ecological footprint data of the world in this lesson. Coming up next, you’ll conclude this course by reviewing Folium and learning how it compares to other Python mapping libraries.

Avatar image for alexco43

alexco43 on May 26, 2024

TypeError: ufunc ‘isnan’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘’safe’‘

Consistently getting this error :(

Become a Member to join the conversation.