開始使用免費開始

繪製 iris 資料的長條圖

在本節的練習中,你會使用植物學家 Edward Anderson 所蒐集、並由史上產出豐富的統計學家之一 Ronald Fisher 推廣而聞名的經典資料集。Anderson 仔細量測了 3 個鳶尾花(iris)物種的形態特徵樣本:Iris setosaIris versicolorIris virginica。完整資料集可於 scikit-learn 提供的套件中取得。在這裡,你將使用他所量測的花瓣長度。

請使用 matplotlib/seaborn 的預設設定,為 Iris versicolor 的 50 個樣本花瓣長度繪製長條圖。回想一下,若要指定 seaborn 的預設樣式,可以使用 sns.set(),其中 sns 是匯入 seaborn 時使用的別名。

包含 Iris versicolor 花瓣長度(單位為公分 cm)的資料子集,已儲存在 NumPy 陣列 versicolor_petal_length 中。

在影片中,Justin 是使用 pandas 函式庫,並透過對 DataFrame 取索引以擷取想要的欄位來繪製長條圖。然而在這裡,你只需要使用提供的 NumPy 陣列即可。另外,Justin 將他的繪圖語句(除了 plt.show() 之外)指派給虛擬變數 _,以避免顯示不必要的輸出。這並不是你解題時的必要步驟,但這是個不錯的習慣。或者,如果你在像 Jupyter notebook 這樣的互動式環境中工作,也可以在繪圖語句後面加上一個 ; 來達到相同效果。Justin 偏好使用 _,因此你會在解答程式碼中看到它。

本練習屬於課程

Statistical Thinking in Python (Part 1)

檢視課程

練習說明

  • 匯入 matplotlib.pyplotseaborn,並使用慣用別名(pltsns)。
  • 使用 seaborn 設定繪圖預設值。
  • 使用 plt.hist() 與提供的 NumPy 陣列 versicolor_petal_length,為 Iris versicolor 的花瓣長度繪製長條圖。
  • 使用 plt.show() 顯示長條圖。

動手互動練習

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

# Import plotting modules



# Set default Seaborn style


# Plot histogram of versicolor petal lengths


# Show histogram


編輯並執行程式碼