開始使用免費開始

箭頭註解

想像你是加州 Long Beach 的市政規劃師。Long Beach 位在太平洋沿岸,每年跨年夜都有大型煙火秀。你想了解這個活動是否對城市的空氣品質有負面影響。為了做到這點,你會查看元旦當天的 CO 與 NO2 濃度。不過,事實上元旦那天在右側的圖中並不是離群值,而是落在比較擁擠的區域。

為了引導讀者注意到這一點,你會加上一個註解,並用箭頭指向元旦的數值。這能在圖中較不擁擠的區域顯示文字,同時提供清楚的說明,幫助讀者理解他們正在看什麼。

本練習屬於課程

用 Python 改善你的資料視覺化

檢視課程

練習說明

  • 使用 pandas 的 .query() 方法,從 jan_pollution 擷取對應到 Long Beach 市 2012 年元旦的那一列資料。
  • lb_newyears DataFrame 中 CONO2 欄位的數值設定箭頭終點(xy)。
  • 使用參數 xytext,把註解箭頭的文字放在畫面左下角,x = 2、y = 15
  • 將箭頭的 'shrink' 設為 0.03,以免遮蔽重點資料點。

動手互動練習

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

# Query and filter to New Years in Long Beach
jan_pollution = pollution.query("(month  ==  1) & (year  ==  2012)")
lb_newyears = jan_pollution.query("(____  ==  ____) & (____  ==  '____')")

sns.scatterplot(x = 'CO', y = 'NO2',
                data = jan_pollution)

# Point arrow to lb_newyears & place text in lower left 
plt.annotate('Long Beach New Years',
             xy = (____, ____),
             xytext = (____, _____), 
             # Shrink the arrow to avoid occlusion
             arrowprops = {'facecolor':'gray', 'width': 3, '____': ____},
             backgroundcolor = 'white')
plt.show()
編輯並執行程式碼