Lecture Note
University
University of California San DiegoCourse
DSC 207R | Python for Data SciencePages
2
Academic year
2023
anon
Views
19
Folium Example Using Folium to Create Powerful Geographic Overlays Are you seeking for a robust data visualization method? It might be overwhelming to work with datasets like the global development indicators. But, with the correct tools, you canproduce interesting and instructive visualizations that aid in data understanding. In thisarticle, we'll look at how to utilize the Folium library to make geographic overlays that mightgive your data fresh perspectives. What is Folium? Folium is a Python library used to create interactive maps. It allows users to create different map types such as choropleth maps, heat maps, and cluster maps. Folium is built on top ofthe Leaflet JavaScript library, which is one of the most popular open-source libraries forcreating interactive maps. Getting Started with Folium Before you can use Folium, you need to install it on your system. You can do this by running the command pip install folium in your terminal. You will also need to download the json file for world countries, which you can find online, and place it in your working directory. Folium may be used to make interactive maps once it has been installed and the world nations json file has been placed in your working directory. Creating a Choropleth Map A map called a choropleth utilizes color to depict data values. Several value ranges can be represented using the color scale. In this illustration, we'll make a choropleth map to showglobal CO2 emissions per person. First, we will read in the world development indicators dataset, which can be found on the World Bank website. We will then extract the CO2 emissions for all countries in 2011, whichwe will use to create our map. import folium
import pandas as pd # Read in the world development indicators dataset data = pd.read_csv('world-development-indicators.csv') # Extract CO2 emissions for all countries in 2011co2_data = data[(data['Indicator Name'] == 'CO2 emissions (metric tonsper capita)') & (data['Year'] == 2011)][['Country Code', 'Value']] Next, we will extract the name of the indicator for use as the legend in the figure. # Extract the name of the indicator for use as the legend in the figure indicator_name = data[(data[ 'Indicator Name' ] == 'CO2 emissions (metric tons per capita)' )][ 'Indicator Name' ].iloc[ 0 ] Now that we have our data, we can create our Folium map. We will tell it to create a map at a fairly high level of zoom and then use the built-in method called choropleth to attach thecountry's geographic json and the plot data. We need to specify the relevant columns, keyon feature ID refers to the label in the json object which has the country code as the featureID attached to each country's border information. Our country code in the data framematches the feature ID in the json object. # Create a Folium map at a fairly high level of zoom map = folium.Map(location=[ 20 , 0 ], zoom_start= 2 ) # Use the built-in method called `choropleth` to attach the country'sgeographic json and the plot data map .choropleth(geo_data= 'geo/world-countries.json' , data=co2_data, columns=[ 'Country Code' , 'Value' ], key_on= 'feature.id' , fill_color= 'YlGnBu' , fill_opacity= 0.7 , line_opacity= 0.2 , legend_name=indicator_name) # Save the output of this plot as an html file map .save( 'CO2_emissions_per_cap
Exploring Geographic Visualization with Folium
Please or to post comments