Customizing the Folium Choropleth
00:00 In this lesson, you’ll continue working with your choropleth by customizing it.
00:07 There are many, many different customizations that you can do in Folium. You can update the color scheme of your choropleth and adjust the bins and name of your color bar.
00:17 You can add markers for places of interest along with tooltips or pop-ups for those markers to explain the meaning of each point. Let’s explore these options and others in the Python code.
00:32 First, let’s update the color palette of our choropleth for the population of each New York City borough. Folium defaults to a sequential blue palette for choropleth layers, but you can choose any Brewer color palette option.
00:47
Let’s use the Yellow-Orange-Red sequential color palette to further emphasize boroughs with larger populations. To switch your choropleth’s color palette, just pass the name of that palette to the fill_color
property.
01:03
Here we’ll use "YlOrRd"
for Yellow-Orange-Red. Now we have a yellow color for Staten Island, which has a smaller population, and red for Brooklyn and Queens, which have larger populations.
01:21
You may also choose to make the fill color of your choropleth more or less transparent. To update the fill opacity, just pass a float ranging between zero and one to the fill_opacity
property.
01:36
A value of 0
will make these regions fully transparent, while a value of 1
makes each region fully opaque. And likewise, you can also adjust the transparency of the border lines of each region with line_opacity
.
01:58 Folium does its best to automatically detect bins for your choropleth color bar, but you can manually set these if you’d like to space them more logically.
02:07
The populations here range between zero and three million, so let’s make the bins of this choropleth evenly spaced at every half million. Just pass a list of where each bin starts and stops to the bins
property.
02:23 Now you’ll see different breakpoints in the color bar, and the colors of the map have also adjusted accordingly, based on where each borough’s population falls in this new scale.
02:36
You can also name your color bar to give your end user more context about these numbers. Just pass a string with your preferred name to the legend_name
property.
02:47
Since these are populations for each borough based on the 2020 US census, let’s call this 'Population (US Census 2020)'
. And now that name appears here, just below the color bar values.
03:03 As you can see, we’re creating quite a bit of code here by adjusting these choropleth options. You definitely don’t need to memorize all these properties.
03:11
Just check the Folium documentation anytime you need a refresher. In a Jupyter Notebook, you can click within the Choropleth
parentheses, hold the Shift key, and press Tab to bring up the signature and docstring.
03:25 This should help refresh your memory anytime you need a reminder.
03:31 Before concluding this lesson, let’s explore one more type of customization for Folium maps: adding markers for places of interest. Say you’d like to indicate where sites like the Empire State Building and the Statue of Liberty are located, and you currently have those sites in a dictionary where the names are the keys and the latitude/longitude coordinates are the values.
03:54 You can add markers for these sites on your Folium choropleth by looping over them. So for each site in the site’s dictionary, you’d like to add a Folium marker.
04:06 The location of that site will be the coordinates, which you can look up, as the values of your site’s dictionary, and you’ll add those markers to your NYC map.
04:18 Excellent. You should see that there are three markers for each of the sites included in your dictionary, but it’s not clear which marker represents which site of interest.
04:31
Folium also allows you to add tooltips to your markers so that text appears when users hover over them. In this case, let’s set tooltip
equal to site
, which will represent the string name of each site as we loop over them.
04:48
Now, you’ll be able to recognize this marker as the Empire State Building and this one as Central Park. Finally, you also have the option to further customize your markers by passing a specific Folium icon to the icon
property.
05:04 Let’s make these markers red to match our color scheme, and we’ll use a star as the inner icon.
05:13 Nice job. In this lesson, you learned all about styling your choropleth, including its color palette and color bar. You also added markers with tooltips to your map to indicate points of interest.
05:28 Coming up next, you’ll put everything you’ve learned together to create a Folium choropleth for the world based on ecological data.
Become a Member to join the conversation.