「Step」直方圖
直方圖可以用來查看不同群組的資料分佈。本練習中,你會從 2016 年夏季奧運會得牌選手的資料集中,選出兩個運動項目,比較得牌選手的身高分佈。
資料儲存在名為 summer_2016_medals 的 pandas DataFrame,其中包含一個「Height」欄位。另外,也提供了一個已依運動項目分組的 pandas GroupBy 物件。
在這個練習中,你會將「Gymnastics」與「Rowing」兩個運動的直方圖視覺化並加上標籤,觀察這兩個項目得牌選手之間明顯的差異。
本練習屬於課程
Matplotlib 資料視覺化入門
練習說明
- 使用
hist方法顯示mens_rowingDataFrame 中"Weight"欄位的直方圖,並標記為「Rowing」。 - 使用
hist顯示mens_gymnasticsDataFrame 中"Weight"欄位的直方圖,並標記為「Gymnastics」。 - 兩個直方圖都使用
histtype引數,將型態設為'step',並將分箱數(bins)設為 5。 - 在顯示圖形之前,為圖形加入圖例。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
fig, ax = plt.subplots()
# Plot a histogram of "Weight" for mens_rowing
____
# Compare to histogram of "Weight" for mens_gymnastics
____
ax.set_xlabel("Weight (kg)")
ax.set_ylabel("# of observations")
# Add the legend and show the Figure
____
plt.show()