繪製插值結果
要比較不同的插補結果,視覺化對照圖非常關鍵,有助於推論出最佳的插補技巧。在這個練習中,你將撰寫一個 for 迴圈,為多種插補方法產生圖表。你會繪製上一個單元中已經完成的插補結果,也就是線性(linear)、二次(quadratic),以及最近鄰(nearest)插值法。
已為你匯入了 3 個以插補方法命名的 DataFrame,分別是 linear_interp、quadratic_interp,以及 nearest_interp。
本練習屬於課程
在 Python 中處理遺漏值
練習說明
- 為每種插補方法建立 3 個子圖(subplot)。
- 建立
interpolations字典,將linear_interp、quadratic_interp和nearest_interp這些 DataFrame 映射到對應的插值方法。 - 以
axes與interpolations進行迴圈。 - 在
interpolations中依序選取各個 DataFrame,並使用df_key為該 DataFrame 設定標題。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Set nrows to 3 and ncols to 1
fig, axes = plt.subplots(___, ___, figsize=(30, 20))
# Create a dictionary of interpolated DataFrames for looping
interpolations = {'Linear Interpolation': ___, 'Quadratic Interpolation': ___,
'Nearest Interpolation': ___}
# Loop over axes and interpolations
for ax, df_key in zip(___, ___):
# Select and also set the title for a DataFrame
interpolations[___].Ozone.plot(color='red', marker='o', linestyle='dotted', ax=ax)
airquality.Ozone.plot(title=___ + ' - Ozone', marker='o', ax=ax)
plt.show()