Choropleths with folium
1. Choropleths with folium
Let's look at one more way to visualize geospatial data in a choropleth using python. In this chapter, you will learn how to create a choropleth with the folium package. With a folium choropleth you can add markers and popups to make the map interactive.2. folium.Map choropleth
You already know how to create a folium map by passing a location and zoom level to the folium-dot-Map constructor. This is the first step in constructing a folium choropleth. After initializing the map, we use the folium-dot-Choropleth constructor to create a choropleth.3. Arguments of the folium choropleth
A folium choropleth can be constructed from a single GeoDataFrame, or from two separate data sources. Let's look at the arguments needed by a folium choropleth. First, you need a source for the polygons. You tell folium this with the geo_data argument. Name specifies where polygons are defined in the geo_data - a column if polygons are in a GeoDataFrame, and a property for a geojson file. Data identifies the source of the normalized data to plot. The columns argument, a list, specifies a column that maps to polygons in geo_data and a column to use for shading each polygon.4. Additional arguments of the folium choropleth
Key_on tells how to bind the data to the geo_data. Folium reverse engineers a GeoDataFrame to GeoJSON, so this will always start with feature Fill_color specifies which palette to use for the choropleth. Folium uses ColorBrewer palettes. Fill_opacity sets the level of transparency for the polygon fill_color. The value ranges from 0 to 1, with zero being completely transparent and one being completely opaque. Line_color and line_opacity set the color and opacity for the polygon outlines. And legend_name will create a title for your legend.5. Folium choropleth of school density
After creating the map object we define the choropleth by passing districts_with_counts, the GeoDataFrame with all merged data, to both the geo_data and data arguments. We tell folium that the polygons are in the geometry column using the name argument and to plot school_density by district with the columns argument. The key-on is set to feature-dot-properties-dot-district, from the GeoJson properties. We set the color and opacity for the polygon fill and the outlines. Finally we give the legend a title, 'Schools per km squared by School District.' We use the add_to() method to add the choropleth to our map object m.6. Folium choropleth of school density
And here's the choropleth. We see the school density with the color gradation, and the legend explains the different shades. We also get a street map from leaflet underneath the choropleth.7. Let's Practice!
In the final exercises for this course, you will create a choropleth using folium to show the density of permitted building projects in each council district. Then you will create a marker at the center of each district with a popup that tells the district number and the count of permits issued between September 2015 and September 2018.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.