In this tutorial, you'll learn how to use Folium to create interactive web maps that can help you visualize geographic data more effectively. Folium is built on the JavaScript library Leaflet.js and it makes it very simple to generate maps directly from data. ### Creating a Basic Map Creating a basic map using Folium is very simple. Below is an example of how you can create a map centered at a specific location.
import folium # Create a map centered at a given location m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) # Display the map m
In this example, the map is centered at the coordinates for Portland, Oregon. ### Adding Markers Markers help to point out specific locations on your map. Here's how you can add a marker to the map.
import folium # Create a basic map m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) # Add a marker to the map marker = folium.Marker([45.5236, -122.6750], popup='Portland, OR').add_to(m) # Display the map m
### Adding Different Types of Objects Folium allows you to add different types of objects to your map such as circles, polygons, and more. Here's how to add a circle to your map.
import folium # Create a basic map m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) # Add a circle to the map circle = folium.Circle( location=[45.5215, -122.6764], radius=500, popup='The Waterfront', color='crimson', fill=True, fill_color='crimson' ).add_to(m) # Display the map m
### Customizing Map Tiles Folium allows you to choose from several tile styles. You can view the full list of providers [here](https://leaflet-extras.github.io/leaflet-providers/preview/){target="_blank"}.
import folium # Create a map with custom tiles m = folium.Map( location=[45.5236, -122.6750], zoom_start=13, tiles='OpenTopoMap', attr='Map data: © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: © <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)' ) # Display the map m
### Adding Choropleth Maps Choropleth maps are excellent for visualizing data across geographical regions. Below is an example of how to create a choropleth map.
import pandas import requests import folium state_geo = requests.get( "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json" ).json() state_data = pandas.read_csv( "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_unemployment_oct_2012.csv" ) map = folium.Map(location=[38, -112], zoom_start=3) folium.Choropleth( geo_data=state_geo, name="choropleth", data=state_data, columns=["State", "Unemployment"], key_on="feature.id", fill_color="YlGn", fill_opacity=0.7, line_opacity=0.2, legend_name="Unemployment Rate (%)", ).add_to(map) folium.LayerControl().add_to(map) map
### Custom Popups Custom popups can make your map more informative and engaging. You can add HTML content or even images inside your popups. #### HTML and Static Image Popups
import folium from folium import IFrame # Create a basic map m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) # Create an HTML popup with static content html = ''' <h3>Portland</h3> <p>Population: 653,115</p> ''' # Create new marker with custom popup iframe = IFrame(html=html, width=200, height=100) popup = folium.Popup(iframe, max_width=2650) marker = folium.Marker(location=[45.5236, -122.6750], popup=popup).add_to(m) # Display the map m
#### Popup with an Image
import folium from folium import IFrame # Create a basic map m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) # URL of an image image_url = "https://plus.unsplash.com/premium_photo-1675122317633-756659fcdf06?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MXx8cG9ydGxhbmR8ZW58MHx8MHx8fDA%3D" # Create HTML to embed the image html = f'<img src="{image_url}" width="100" />' # Create new marker with custom popup iframe = IFrame(html=html, width=150, height=150) popup = folium.Popup(iframe, max_width=2650) marker = folium.Marker(location=[45.5236, -122.6750], popup=popup).add_to(m) # Display the map m
### Advanced Layer Control Folium allows you to manage multiple layers effectively, enabling better control over how information is displayed. #### Adding Layer Control
import folium # Create a basic map m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) # Define layers base_layer = folium.FeatureGroup(name='Base Layer') marker_layer = folium.FeatureGroup(name='Marker Layer') # Add the base layer base_layer.add_to(m) # Add markers to the marker layer folium.Marker([45.5236, -122.6750], popup='Portland').add_to(marker_layer) folium.Marker([45.5017, -122.6652], popup='Marker 2').add_to(marker_layer) # Add the marker layer to the map marker_layer.add_to(m) # Add layer control to turn layers on and off folium.LayerControl().add_to(m) # Display the map m
### More Complex Visualizations In this section, let's create a more complex visualization by combining multiple features such as choropleths with custom popups. #### Complex Map Visualization Example
import folium import pandas as pd import requests # Sample data for choropleth map data = pd.DataFrame({ 'State': ['California', 'New York'], 'Value': [39.12, 19.80] }) # GeoJSON data to use as a layer state_geo = requests.get( "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json" ).json() # Create the basic map m = folium.Map(location=[38, -100], zoom_start=4) # Add choropleth map layer choropleth = folium.Choropleth( geo_data=state_geo, name='choropleth', data=data, columns=['State', 'Value'], key_on='feature.properties.name', fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2 ).add_to(m) # Custom markers with popups folium.Marker( location=[37.7749, -122.4194], popup=folium.Popup('<strong>California</strong><br>Population: 39,128,162', max_width=150) ).add_to(m) folium.Marker( location=[40.7128, -74.0060], popup=folium.Popup('<strong>New York</strong><br>Population: 19,795,791', max_width=150) ).add_to(m) # Add layer control folium.LayerControl().add_to(m) # Display the map m
### Conclusion Folium is a very flexible and powerful library for creating interactive maps. With just a few lines of code, you can create comprehensive visualizations that integrate multiple layers of geographic data.