開始使用免費開始

視覺化人口密度

回到行政區資料集。在先前的練習裡,我們用單一顏色顯示所有行政區。不過,很多時候我們想呈現某個變數在空間上的差異,並依此為多邊形上色。

在這個練習中,你會視覺化巴黎市中心內人口密度的空間差異。為此,我們會先把人口數除以面積以計算人口密度,並將結果新增為 dataframe 的一個新欄位。

行政區資料集已載入為 districts,GeoPandas 已以 geopandas 匯入,matplotlib.pyplot 以 plt 匯入。

本練習屬於課程

在 Python 中處理地理空間資料

檢視課程

練習說明

  • 印出行政區資料集的前幾列。你有看到「'population'」欄嗎?
  • 檢視各行政區幾何的面積。
  • 新增一個「'population_density'」欄,表示每平方公里的居民數(注意:面積的單位是平方公尺,所以你需要將結果乘上 10**6)。
  • 以「'population_density'」為著色依據,繪製行政區多邊形。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Inspect the first rows of the districts dataset
print(districts.head())

# Inspect the area of the districts
print(districts.____)

# Add a population density column
districts['population_density'] = ____ / ____ * ____

# Make a plot of the districts colored by the population density
districts.____(____, legend=True)
plt.show()
編輯並執行程式碼