You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python绘制类似示例的风速风向时序图?

风速风向时间序列可视化方案(匹配目标样式)

核心思路

目标图应为时间轴上每个节点用**带方向的箭头(表示风向)+ 箭头长度/风羽(表示风速)**的形式展示,以下提供两种可落地的实现方案:


方案一:Matplotlib 自定义绘制(精准匹配样式)

步骤1:数据预处理

气象风向是「风的来向」,而Matplotlib的角度以x轴正方向逆时针计算,需先做转换:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 假设你的数据集存储在df中
# 将气象风向转为Matplotlib可识别的角度
df['wind_dir_rad'] = np.radians(90 - df['wind_dir'])

步骤2:绘制风向箭头+风速映射

用quiver函数绘制箭头,箭头长度对应风速,方向对应转换后的风向:

plt.figure(figsize=(12, 6))
# 将时间序列转为数值索引,方便x轴定位
x = np.arange(len(df))
y = [0] * len(df)  # 固定y轴位置,让箭头横向排列

plt.quiver(x, y,
           df['wind_speed'] * np.cos(df['wind_dir_rad']),
           df['wind_speed'] * np.sin(df['wind_dir_rad']),
           scale=50,  # 根据你的风速范围调整,值越大箭头越短
           width=0.005,
           color='darkblue')

# 格式化时间轴标签
plt.xticks(x, df['time'].dt.strftime('%Y-%m-%d %H:%M'), rotation=45)
plt.yticks([])  # 隐藏y轴刻度
plt.xlabel('时间')
plt.title('时间序列风速风向图')
plt.tight_layout()
plt.show()

如果要实现风羽样式,可替换为调整参数后的barbs函数:

plt.figure(figsize=(12, 6))
plt.barbs(x, y,
          df['wind_speed'] * np.cos(df['wind_dir_rad']),
          df['wind_speed'] * np.sin(df['wind_dir_rad']),
          length=8,  # 风羽长度
          barbcolor='black',
          flagcolor='red',
          linewidth=1.2)
plt.xticks(x, df['time'].dt.strftime('%Y-%m-%d %H:%M'), rotation=45)
plt.yticks([])
plt.tight_layout()
plt.show()

方案二:Plotly 交互式可视化(美观易调整)

用Plotly的散点图,通过标记符号表示风向、标记大小表示风速:

import plotly.express as px

# 映射风向到Plotly箭头符号(按需调整角度对应关系)
df['arrow_sym'] = df['wind_dir'].apply(lambda x: [
    'arrow-up', 'arrow-up-right', 'arrow-right', 'arrow-down-right',
    'arrow-down', 'arrow-down-left', 'arrow-left', 'arrow-up-left'
][int(x/45) % 8])
df['marker_size'] = df['wind_speed'] * 2  # 风速映射为标记大小

fig = px.scatter(df, x='time', y=[0]*len(df),
                 size='marker_size',
                 color='wind_speed',
                 symbol='arrow_sym')
fig.update_layout(yaxis_visible=False, xaxis_title='时间', title='风速风向时间序列')
fig.show()

关键注意事项

  • 风向角度转换:务必区分气象风向(来向)和绘图库箭头方向(去向),转换错误会导致风向完全相反。
  • 风速缩放:根据数据集的风速范围,调整scale(Matplotlib)或marker_size(Plotly)的比例,保证箭头/风羽大小适中。
  • 时间轴优化:若time为datetime类型,用dt.strftime格式化标签,避免文本重叠。

内容的提问来源于stack exchange,提问作者matrix_season

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:00:58