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

如何提取PyVista plot_over_line函数绘制的XY数据?

问题

我有一组CFD仿真生成的.vtk格式3D数据,想用PyVista的plot_over_line功能绘制某组点数据的值。按照官方示例的方法操作时运行正常:

# Load mesh
mesh = examples.download_kitchen()
# Make points
a = [mesh.bounds[0], mesh.bounds[2], mesh.bounds[4]]
b = [mesh.bounds[1], mesh.bounds[3], mesh.bounds[5]]
# Make line
line = pv.Line(a, b)
# Plot
test = mesh.plot_over_line(a, b, resolution=100)

但我希望提取plot_over_line所绘制的XY数据,以便后续进行更灵活的绘图操作(比如调整颜色、和其他方案对比等),该怎么实现?

解决方案

plot_over_line函数返回的是一个pyvista.UnstructuredGrid对象,里面包含了所有采样点的路径距离和对应的数据值,直接从这个对象里提取即可,步骤如下:

  1. 获取采样数据(不自动绘图)
    调用plot_over_line时添加plot=False参数,避免自动弹出绘图窗口,只返回采样结果:

    sampled_data = mesh.plot_over_line(a, b, resolution=100, plot=False)
    
  2. 提取X轴数据(沿线路径的距离)
    采样结果中自带Distance字段,记录每个采样点到起点的直线距离:

    distance = sampled_data.point_arrays['Distance']
    
  3. 提取Y轴数据(目标标量值)
    先查看所有可用的点数据字段,再提取目标字段的值:

    # 查看所有可提取的点数据字段
    print(sampled_data.point_arrays.keys())
    # 替换为你的VTK数据中的目标字段名
    scalar_values = sampled_data.point_arrays['你的数据字段名']
    
  4. 自定义绘图示例
    用Matplotlib等工具自行绘制,灵活调整样式:

    import matplotlib.pyplot as plt
    
    plt.plot(distance, scalar_values, color='#1f77b4', linewidth=2, linestyle='-')
    plt.xlabel('距离(沿路径)')
    plt.ylabel('标量值')
    plt.title('沿路径的CFD数据分布')
    plt.grid(alpha=0.3)
    plt.show()
    

额外说明

  • 如果需要用采样点的三维坐标(而非距离)作为X轴,可直接提取点坐标:
    points = sampled_data.points
    # 比如取X轴坐标作为横轴
    x_coords = points[:, 0]
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 08:10:15