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

如何使用Quiver箭袋图在矢量场中展示速度大小?

问题描述

我已经获取了二维网格上指定点位的x、y方向速度分量数据,可通过ff.create_quiver(X, Y, UN, VN)实现速度矢量可视化,其中UN、VN为各速度的方向分量。但我不清楚如何将每个速度对应的speed(速度大小)和对应箭头同步展示。
现有代码运行输出的可视化效果如下:
可视化结果示例
现有代码:

import plotly.figure_factory as ff
import plotly.graph_objs as go
import numpy as np

#creating grid
X,Y = np.meshgrid(np.arange(0,11,1),np.arange(0, 11, 1))

#basic vector calculus
Ux = X/np.sqrt(X**2 + Y**2) #velocity in x direction
Uy = Y/np.sqrt(X**2 + Y**2) #velocity in y direction
speed = np.sqrt(Ux**2 + Uy**2) #VELOCITY MAGNITUDE 
UN = Ux/speed # velocity direction
VN = Uy/speed # velocity direction
f = ff.create_quiver(X, Y, UN, VN,
                       scale=.6,
                       arrow_scale=.5,
                       name='quiver',
                       line_width=2, line_color='black')

# u can ignore these codes below (it's for temperature visualization)
temperature = f.data[0]
trace2 = go.Contour(
   
       z= np.random.random((12, 12))+23,
        colorbar={"title": 'Temperature'},
        colorscale='jet',opacity=0.7
   )
data=[temperature,trace2]
fig = go.FigureWidget(data)
fig.update_layout(title='Room airflow velocity and temperature distribution',
                  title_x=0.5,
                  title_y=0.85,
                  xaxis_title="Room length",
                  yaxis_title='Room width',
                 font_size=15,font_family="Times New Roman")
fig.show()

解决方案

可以通过给箭头线段绑定速度大小的颜色映射实现两者同步展示,同时可配置hover信息显示具体速度数值,修改后代码如下:

import plotly.figure_factory as ff
import plotly.graph_objs as go
import numpy as np

# 创建网格
X,Y = np.meshgrid(np.arange(0,11,1),np.arange(0, 11, 1))
# 处理原点除零问题
X[X==0] = 1e-6
Y[Y==0] = 1e-6

# 基础矢量计算
Ux = X/np.sqrt(X**2 + Y**2) # x方向速度
Uy = Y/np.sqrt(X**2 + Y**2) # y方向速度
speed = np.sqrt(Ux**2 + Uy**2) # 速度大小
UN = Ux/speed # 速度x方向分量
VN = Uy/speed # 速度y方向分量

# 生成箭图
f = ff.create_quiver(X, Y, UN, VN,
                       scale=.6,
                       arrow_scale=.5,
                       name='气流速度',
                       line_width=2)

# 给箭头绑定速度大小颜色映射
# 每个箭头对应3个线段点,需要把speed值重复3次匹配线段长度
speed_mapped = np.repeat(speed.flatten(), 3)
f.data[0].line.color = speed_mapped
f.data[0].line.colorscale = 'Reds'
f.data[0].line.colorbar = dict(title="速度大小", x=1.1)
# 配置hover信息
f.data[0].hovertemplate = 'X坐标: %{x}<br>Y坐标: %{y}<br>速度大小: %{line.color:.2f}<extra></extra>'

# 温度可视化部分
trace2 = go.Contour(
       z= np.random.random((12, 12))+23,
        colorbar={"title": '温度', "x":1.2},
        colorscale='jet',opacity=0.7
   )
data=[f.data[0], trace2]
fig = go.FigureWidget(data)
fig.update_layout(title='室内气流速度与温度分布',
                  title_x=0.5,
                  title_y=0.85,
                  xaxis_title="房间长度",
                  yaxis_title='房间宽度',
                 font_size=15,font_family="Times New Roman")
fig.show()

效果说明

  • 箭头颜色会根据对应点位的速度大小自动变化,颜色条对应速度数值区间
  • 鼠标悬停在箭头上时会自动显示当前点位的坐标和速度大小
  • 额外修复了原点除零报错的问题,避免运行异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 21:36:03