自定义坐标轴标签并添加标题
要自定义坐标轴标签,需要使用 Axes 对象的 set_xlabel 和 set_ylabel 方法。添加标题使用 set_title 方法。
在本练习中,您将自定义坐标轴标签的内容,并为图表添加标题。
与之前一样,数据已作为 pandas DataFrame 对象加载到内存中:seattle_weather 和 austin_weather。它们各自包含 "MONTH" 列和 "MLY-PRCP-NORMAL" 列。示例代码前两行已将这些数据相互作图。
此外,名为 fig 的 Figure 对象和名为 ax 的 Axes 对象已经为您创建好了。
本练习是课程的一部分
Matplotlib 数据可视化入门
练习说明
- 使用
set_xlabel方法添加标签:"Time (months)"。 - 使用
set_ylabel方法添加标签:"Precipitation (inches)"。 - 使用
set_title方法添加标题:"Weather patterns in Austin and Seattle"。
交互式实操练习
通过完成这段示例代码来试试这个练习。
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-PRCP-NORMAL"])
ax.plot(austin_weather["MONTH"], austin_weather["MLY-PRCP-NORMAL"])
# Customize the x-axis label
____
# Customize the y-axis label
____
# Add the title
____
# Display the figure
plt.show()