改進你的 KDE
提升 KDE 的一種方式是加入「地毯圖」(rug plot)。地毯圖會在密度曲線下方畫出小刻痕,精確標示每一筆資料的位置。當你的資料量不大時,加入地毯圖特別有幫助。
當資料量很少時,常會在支撐範圍上出現沒有資料的空隙,而你可能很難判斷非零的 KDE 線段是因為真的有資料,還是因為使用了較寬的核心函式。地毯圖能幫助你分辨這點。
我們回到 sns.kdeplot() 函式,繪製兩條 KDE:一條是 Vandenberg Air Force Base 的資料,另一條是汙染資料中所有其他城市的資料。由於 Vandenberg 圖形的形狀來自的資料明顯比較少,請在它的下方加上一個地毯圖。
本練習屬於課程
用 Python 改善你的資料視覺化
練習說明
- 將 Vandenberg 的圖設為
'steelblue'。 - 在 Vandenberg 的圖中啟用地毯圖功能。
- 將非 Vandenberg 的圖設為
'gray'。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
sns.kdeplot(pollution[pollution.city == 'Vandenberg Air Force Base'].O3,
label = 'Vandenberg',
# Turn the color blue to stand out
color = '____')
# Turn on rugplot
sns.____(pollution[pollution.city == 'Vandenberg Air Force Base'].O3,
label = 'Vandenberg',
color = 'steelblue')
sns.kdeplot(pollution[pollution.city != 'Vandenberg Air Force Base'].O3,
label = 'Other cities',
# Turn the color gray
color = '____')
plt.show()