展示某一气象要素(如温度、降水量、风速等)随时间变化的图表,常用于分析季节变化、长期趋势或极端天气事件。
气温
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 假设你有一个包含日期和气温的数据集,这里我们创建一些模拟数据
# 日期范围(例如,一年的时间)
dates = pd.date_range('20230101', periods=365)
# 气温数据(随机生成,仅作为示例)
temperatures = np.random.uniform(low=5, high=30, size=365)
# 将数据转换为DataFrame,以便更容易地处理
df = pd.DataFrame({'Date': dates, 'Temperature': temperatures})
# 绘制时间序列图
plt.figure(figsize=(10, 5)) # 设置图形的大小
plt.plot(df['Date'], df['Temperature'], marker='o', linestyle='-', color='b') # 绘制折线图
# 添加标题和坐标轴标签
plt.title('Temperature Time Series')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
# 设置日期格式(可选,取决于你的具体需求)
plt.gcf().autofmt_xdate() # 自动旋转日期标签以避免重叠
# 显示图形
plt.grid(True) # 显示网格线
plt.show()
降水量
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# 假设你有一个包含日期和降水量的数据集,这里我们创建一些模拟数据
# 日期范围(例如,一年的时间)
dates = pd.date_range('20230101', periods=365)
# 降水量数据(随机生成,仅作为示例)
precipitations = np.random.randint(0, 50, size=365) # 假设降水量在0到50之间
# 将数据转换为DataFrame,以便更容易地处理
df = pd.DataFrame({'Date': dates, 'Precipitation': precipitations})
# 设置日期为DataFrame的索引,这样在绘图时可以更方便地处理日期
df.set_index('Date', inplace=True)
# 绘制时间序列图
plt.figure(figsize=(10, 5)) # 设置图形的大小
plt.plot(df.index, df['Precipitation'], marker='o', linestyle='-', color='b') # 绘制折线图
# 添加标题和坐标轴标签
plt.title('Precipitation Time Series')
plt.xlabel('Date')
plt.ylabel('Precipitation (mm)')
# 设置日期格式(可选,但通常是个好习惯)
plt.gcf().autofmt_xdate() # 注意:这里应该是plt.gcf(),但通常我们使用plt.gca()来获取当前轴并调用autofmt_xdate
# 更正为:plt.gca().xaxis.set_major_formatter(plt.DateFormatter('%Y-%m-%d')) # 如果你需要特定格式的日期
# 或者,如果你只是想让日期标签不重叠,可以使用plt.xticks(rotation=45)来旋转x轴标签
plt.xticks(rotation=45)
# 显示网格线(可选)
plt.grid(True)
风速时间序列图
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# 创建模拟数据
dates = pd.date_range('20230101', periods=100, freq='H') # 100个小时的日期范围
wind_speeds = np.random.uniform(low=0, high=20, size=100) # 100个风速数据点
# 将数据转换为DataFrame
df = pd.DataFrame({
'Date': dates,
'Wind Speed (m/s)': wind_speeds
})
# 绘制风速时间序列图
plt.figure(figsize=(10, 5))
plt.plot(df['Date'], df['Wind Speed (m/s)'], marker='o', linestyle='-', color='b')
# 添加标题和坐标轴标签
plt.title('Wind Speed Time Series')
plt.xlabel('Date')
plt.ylabel('Wind Speed (m/s)')
# 旋转x轴标签以避免重叠
plt.xticks(rotation=45)
# 显示网格线(可选)
plt.grid(True)
# 显示图形
plt.tight_layout() # 自动调整子图参数, 使之填充整个图像区域
plt.show()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import FancyArrowPatch
from matplotlib.collections import PatchCollection
# 定义风速和风向
wind_speeds = np.array([0.2, 1.6, 0.5, 0.6, 1.3])
wind_directions = np.array([32, 14, 28, 34, 69])
# 假设这些风速和风向对应于等间隔的时间点(例如,每小时)
times = np.arange(len(wind_speeds))
# 初始化图形
fig, ax = plt.subplots(figsize=(10, 5))
# 创建一个箭头集合
arrows = []
# 遍历每个时间点,绘制箭头
for t, speed, direction in zip(times, wind_speeds, wind_directions):
# 将风向角度(度)转换为弧度,并计算笛卡尔坐标系中的偏移量
# 注意:matplotlib的x轴向右为正,y轴向上为正
# 这里我们假设风向图中的0度是向北(y轴正方向),但输入的风向角度是相对于某个方向的
# 通常情况下,我们不需要减去90度,除非你的风向数据是以不同的基准方向给出的
rad = np.deg2rad(direction)
dx = np.cos(rad) * speed # x轴偏移量
dy = np.sin(rad) * speed # y轴偏移量
# 创建一个FancyArrowPatch对象
# 注意:mutation_scale用于控制箭头的大小,我们将其与风速成正比
# 这里我们选择一个合适的比例因子来使箭头在图中清晰可见
a = FancyArrowPatch((t, 0), (t + dx, dy),
arrowstyle="->", mutation_scale=0.1* speed, lw=1, color='b', alpha=0.75)
arrows.append(a)
# 将箭头添加到PatchCollection中,然后添加到轴上
pc = PatchCollection(arrows, match_original=True)
ax.add_collection(pc)
# 设置坐标轴
ax.set_xlim(times[0] - 0.5, times[-1] + 0.5) # 为箭头起点和终点留出一些空间
ax.set_ylim(-max(wind_speeds) * 1.5, max(wind_speeds) * 1.5) # 根据最大风速设置y轴范围
ax.set_xlabel('Time Index') # 时间标签,这里用“Time Index”表示时间点的索引
ax.set_ylabel('Wind Speed and Direction') # y轴标签,表示风速和风向
ax.set_title('Wind Speed and Direction Time Series') # 图表标题
# 隐藏y轴刻度(因为这里y轴只是表示方向和速度的大小,不是实际的数值)
ax.get_yaxis().set_ticks([])
ax.get_xaxis().set_ticks(times) # 可选:显示x轴的时间点索引作为刻度
# 显示网格(可选)
ax.grid(True)
# 显示图形
plt.show()