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

基于PyVista的.vtk数据可视化:自定义网格线技术问询

PyVista自定义网格刻度与样式调整方案

修改后的完整代码

import numpy as np
import pyvista as pv

# 启动虚拟帧缓冲(按需使用)
pv.start_xvfb()

# 加载VTK文件
file_path = 'Geophysical_Data/Well_Locations/Louse_Creek_Wells/RW01/RW01_Updated.vtk'
mesh = pv.read(file_path)

# 打印网格信息与数据数组
print(mesh)
print("点数据数组:", mesh.point_data.keys())
print("单元数据数组:", mesh.cell_data.keys())
print("示例点:\n", mesh.points[:10])

# 交换Y和Z坐标
transformed_points = mesh.points.copy()
transformed_points[:, [1, 2]] = transformed_points[:, [2, 1]]
mesh.points = transformed_points

# 创建绘图器
plotter = pv.Plotter()

# 添加电阻率数据可视化
plotter.add_mesh(
    mesh,
    scalars='Resistivity(ohm.m)',
    cmap='viridis',
    clim=[0, 100],
    scalar_bar_args={'title': 'Resistivity (ohm.m)', 'font_size': 12}
)

# 添加参考线
line_start = np.array([18.5, -0.76, 0])
line_end = np.array([18.5, -0.76 + 1.13, 0])
line_points = np.array([line_start, line_end])
plotter.add_lines(line_points, color='black', width=8)

# 设置XY视角
plotter.view_xy()

# 自定义网格与刻度
plotter.show_grid(
    location="outer",
    color="black",
    xlabel="Distance (m)",
    ylabel="Elevation (m)",
    zlabel="Elevation (m)",
    # 自定义刻度位置
    xticks=[0, 5, 10, 15, 20, 25],
    yticks=[0, -1, -2, -3, -4, -5, -6],
    # 调整网格线宽度
    line_width=1.5,
    # 调整文本(轴标签、刻度标签)大小
    font_size=12,
    # 可选:隐藏Z轴(因为用了XY视角,Z轴不需要显示)
    show_zaxis=False
)

# 显示绘图
plotter.show()

关键修改说明

  • 自定义刻度位置:通过xticks和yticks参数直接传入目标刻度列表,PyVista会自动在指定位置生成刻度线与标签。
  • 调整网格线尺寸:使用line_width参数设置网格线的粗细,数值越大线条越粗。
  • 调整文本大小:font_size参数统一控制轴标签和刻度标签的字体大小;如果需要单独调整颜色或样式,可以通过label_color等参数进一步定制。
  • 优化Z轴显示:因为使用了view_xy(),Z轴无需显示,添加show_zaxis=False可以隐藏Z轴的网格与标签,让画面更简洁。
  • 修复重复代码:移除了原代码中重复的加载与可视化逻辑,保持代码简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 01:35:56