使用 folium 繪製色階地圖

在 Python 視覺化地理空間資料

Mary van Valkenburg

Data Science Program Manager, Nashville Software School

folium.Map 色階地圖

# Construct a map object for Nashville
nashville = [36.1636,-86.7823]
m = folium.Map(location=nashville, zoom_start=10)

# Create a choropleth using the folium Choropleth class
folium.Choropleth(...)
在 Python 視覺化地理空間資料

folium 色階地圖的參數

  • geo_data - 多邊形來源資料(geojson 檔或 GeoDataFrame)
  • name - 多邊形的幾何欄位名稱(或 geojson 屬性)
  • data - 正規化數值的來源 DataFrame 或 Series
  • columns - 兩欄清單:一欄對應多邊形,一欄為要繪製的值
在 Python 視覺化地理空間資料

folium 色階地圖的其他參數

  • key_on - 用來繫結資料的 GeoJSON 變數(一律以 feature 開頭)
  • fill_color - 多邊形填色(預設為藍色)
  • fill_opacity - 介於 0(透明)到 1(不透明)的填色透明度
  • line_color - 多邊形邊線顏色(預設為黑色)
  • line_opacity - 介於 0(透明)到 1(不透明)的邊線透明度
  • legend_name - 圖例標題
在 Python 視覺化地理空間資料
# Center point and map for Nashville
nashville = [36.1636,-86.7823]
m = folium.Map(location=nashville, zoom_start=10)
# Define a choropleth layer for the map
folium.Choropleth(
    geo_data=districts_with_counts,
    name='geometry',
    data=districts_with_counts,
    columns=['district', 'school_density'],
    key_on='feature.properties.district',
    fill_color='YlGn',
    fill_opacity=0.75,
    line_opacity=0.5,
    legend_name='Schools per km squared by School District'
   ).add_to(m)

folium.LayerControl().add_to_map()
display(m)
在 Python 視覺化地理空間資料

以 folium 呈現學校密度的色階地圖

帶色階圖層的學校密度 folium 地圖

在 Python 視覺化地理空間資料

一起來練習吧!

在 Python 視覺化地理空間資料

Preparing Video For Download...