調整長條圖的分箱數
你剛繪製的長條圖有 10 個分箱。這是 matplotlib 的預設值。「平方根法則」是常用來挑選分箱數的經驗法則:把分箱數設為樣本數的平方根。請再次繪製「Iris versicolor」花瓣長度的長條圖,這次用平方根法則來決定分箱數。你可以在 plt.hist() 中用關鍵字引數 bins 指定分箱數。
繪圖工具已經匯入,且已套用 seaborn 的預設樣式。變數 versicolor_petal_length 包含花瓣長度的陣列,已在你的命名空間中可用。
本練習屬於課程
Statistical Thinking in Python (Part 1)
練習說明
- 匯入
numpy並命名為np。這會讓你可以使用平方根函式np.sqrt()。 - 使用
len()判斷你有多少筆資料點。 - 依平方根法則計算分箱數。
- 使用內建的
int()函式將分箱數轉為整數。 - 繪製長條圖,並確實使用
bins關鍵字引數。 - 按下送出後就會繪出圖形,看看你的成果!
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import numpy
# Compute number of data points: n_data
# Number of bins is the square root of number of data points: n_bins
# Convert number of bins to integer: n_bins
# Plot the histogram
# Label axes
_ = plt.xlabel('petal length (cm)')
_ = plt.ylabel('count')
# Show histogram
plt.show()