matplotlib

怎么使用matplotlib绘制天气折线图

小亿
110
2023-09-20 00:10:40
栏目: 编程语言

使用matplotlib绘制天气折线图的基本步骤如下:

  1. 导入所需的包和模块:
import matplotlib.pyplot as plt
  1. 创建一个图表和一个子图:
fig, ax = plt.subplots()
  1. 准备数据,包括时间序列和相应的天气数据:
time = [1, 2, 3, 4, 5]  # 时间序列
weather = [20, 22, 19, 25, 23]  # 天气数据
  1. 绘制折线图:
ax.plot(time, weather)
  1. 添加标题和坐标轴标签:
ax.set_title('Weather Chart')
ax.set_xlabel('Time')
ax.set_ylabel('Temperature')
  1. 显示图表:
plt.show()

完整的代码示例:

import matplotlib.pyplot as plt
# 创建图表和子图
fig, ax = plt.subplots()
# 准备数据
time = [1, 2, 3, 4, 5]  # 时间序列
weather = [20, 22, 19, 25, 23]  # 天气数据
# 绘制折线图
ax.plot(time, weather)
# 添加标题和坐标轴标签
ax.set_title('Weather Chart')
ax.set_xlabel('Time')
ax.set_ylabel('Temperature')
# 显示图表
plt.show()

运行以上代码,即可绘制出天气折线图。你可以根据实际情况修改时间序列和天气数据,以及自定义图表标题和坐标轴标签。

0
看了该问题的人还看了