使用 plt.subplots 创建小 multiples
小 multiples 用于并排绘制多个数据集。在 Matplotlib 中,可以使用 plt.subplots() 函数创建小 multiples。第一个参数是生成的 Axes 对象数组的行数,第二个参数是列数。在本练习中,您将使用奥斯汀和西雅图的数据来练习创建并填充一个子图数组。
数据以 DataFrame 的形式提供:seattle_weather 和 austin_weather。它们各自包含 "MONTH" 列、"MLY-PRCP-NORMAL"(平均降水量)以及 "MLY-TAVG-NORMAL"(平均气温)列。在本练习中,您将分别在不同的子图中绘制每个城市的月均降水量和月均气温。
本练习是课程的一部分
Matplotlib 数据可视化入门
练习说明
- 创建一个 Figure,以及一个包含 2 行 2 列子图的数组。
- 将左上角的 Axes 视为索引 0, 0,绘制西雅图的降水量。
- 在右上角(索引 0, 1)绘制西雅图的气温。
- 在左下角(1, 0)和右下角(1, 1)分别绘制奥斯汀的降水量和气温。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a Figure and an array of subplots with 2 rows and 2 columns
fig, ax = plt.subplots(____, ____)
# Addressing the top left Axes as index 0, 0, plot month and Seattle precipitation
ax[0, 0].plot(____, ____)
# In the top right (index 0,1), plot month and Seattle temperatures
ax[0, 1].plot(____, ____)
# In the bottom left (1, 0) plot month and Austin precipitations
ax[____].plot(____, ____)
# In the bottom right (1, 1) plot month and Austin temperatures
ax[____].plot(____, ____)
plt.show()