开始使用免费开始使用

共享 y 轴的小倍数图

在创建小倍数图时,通常希望不同子图的 y 轴使用相同的刻度范围。可以通过将 sharey 关键字设置为 True 来实现。

在本练习中,您将创建一个包含两个共享 y 轴的 Axes 对象的 Figure。与之前一样,数据位于 seattle_weatheraustin_weather 两个 DataFrame 中。

本练习是课程的一部分

Matplotlib 数据可视化入门

查看课程

练习说明

  • 创建一个包含两个 Axes 对象数组的 Figure,并共享它们的 y 轴范围。
  • 在上方的 Axes 中,用蓝色实线绘制西雅图的 "MLY-PRCP-NORMAL"
  • 在上方的 Axes 中,用蓝色虚线添加西雅图的 "MLY-PRCP-25PCTL""MLY-PRCP-75PCTL"
  • 在下方的 Axes 中,用红色实线绘制奥斯汀的 "MLY-PRCP-NORMAL",并用红色虚线绘制 "MLY-PRCP-25PCTL""MLY-PRCP-75PCTL"

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create a figure and an array of axes: 2 rows, 1 column with shared y axis
fig, ax = plt.subplots(2, 1, sharey=True)

# Plot Seattle precipitation data in the top axes
____.plot(____, ____, color = ____)
____.plot(____, ____, color = ____, linestyle = ____)
____.plot(____, ____, color = ____, linestyle = ____)

# Plot Austin precipitation data in the bottom axes
____.plot(____, ____, color = ____)
____.plot(____, ____, color = ____, linestyle = ____)
____.plot(____, ____, color = ____, linestyle = ____)

plt.show()
编辑并运行代码