開始使用免費開始

繪製質心座標

由於我們無法直接繪出整個外接框(bounding box),所以會用一個稱為質心(centroid)的單一點來概括其位置。把這些點畫在 Basemap 地圖上很直覺。計算出質心之後,將經度與緯度分開,再傳給 .scatter() 方法即可。

Basemap 物件 m 已經為你建立。資料集 tweets_sotu 與函式 calculateCentroid() 也都已載入。

本練習屬於課程

使用 Python 分析社群媒體資料

檢視課程

練習說明

  • 計算質心並存入 centroids
  • fillcontinents 中設定引數 zorder,讓大陸多邊形顯示在點的後面。
  • 繪製這些點。記得將 latlon 引數設為正確的值。

動手互動練習

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

# Calculate the centroids for the dataset
# and isolate longitudue and latitudes
centroids = ____[____].apply(____)
lon = [x[0] for x in centroids]
lat = [x[1] for x in centroids]

# Draw continents, coastlines, countries, and states
m.fillcontinents(color='white', ____ = ____)
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
m.drawstates(color='gray')

# Draw the points and show the plot
____.____(____, ____, ____ = True, alpha = 0.7)
plt.show()
編輯並執行程式碼