开始使用免费开始使用

"阶梯"直方图

直方图可以让我们查看数据中各组的分布。在本练习中,您将从 2016 年里约夏季奥运会奖牌获得者的数据集中选择分组,对比两项不同运动中获奖运动员的身高。

数据存放在名为 summer_2016_medals 的 pandas DataFrame 中,其中包含 "Height" 列。此外,已为您提供按运动项目分组的 pandas GroupBy 对象。

本练习中,您将可视化并标注两项运动的直方图:"Gymnastics" 和 "Rowing",以观察这两项运动的奖牌获得者在身高上的显著差异。

本练习是课程的一部分

Matplotlib 数据可视化入门

查看课程

练习说明

  • 使用 hist 方法显示 mens_rowing DataFrame 中 "Weight" 列的直方图,并将其标签设为 "Rowing"
  • 使用 hist 显示 mens_gymnastics DataFrame 中 "Weight" 列的直方图,并将其标签设为 "Gymnastics"
  • 对这两个直方图,都使用 histtype 参数将可视化类型设为 'step',并将箱数设置为 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()
编辑并运行代码