Web Maps with Folium

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
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
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
import folium

# Create a map with custom tiles
m = folium.Map(
  location=[45.5236, -122.6750], 
  zoom_start=13, 
  tiles='OpenTopoMap',  
  attr='Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <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
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
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
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
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
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
CTRL + ENTER to send