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

如何在Google Colaboratory中用Python可视化3D核?求Mayavi替代库

问题:Google Colab中无法安装wxPython,求3D核数组可视化替代库

我尝试在Google Colaboratory中用Python可视化3D核数组,一开始想用Mayavi库,但安装它依赖的wxPython时失败了。执行的安装命令:

pip install attrdict3
pip install requests
pip install wxpython

出现的错误:

Building wheels for collected packages: wxpython
error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
Building wheel for wxpython (setup.py) ... error
ERROR: Failed building wheel for wxpython
Running setup.py clean for wxpython
Failed to build wxpython
Installing collected packages: wxpython
error: subprocess-exited-with-error

× Running setup.py install for wxpython did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
Running setup.py install for wxpython ... error
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> wxpython

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

我原本的可视化代码:

from mayavi import mlab
mlab.volume_slice(kernel, plane_orientation='x_axes', slice_index=3)
mlab.volume_slice(kernel, plane_orientation='y_axes', slice_index=3)
mlab.volume_slice(kernel, plane_orientation='z_axes', slice_index=3)
mlab.show()

mlab.contour3d(kernel)
mlab.show()

待可视化的3D核数组:

import numpy as np

kernel = np.array([[[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]],

             [[0.88369477, 0.9201748 , 0.5593715 ],
              [0.91746247, 0.9553365 , 0.5807462 ],
              [0.88369477, 0.9201748 , 0.5593715 ]],

             [[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]]])

求推荐适合这个场景的其他可视化库。


推荐替代库及实现示例

以下几个库无需依赖wxPython,且能在Google Colab中正常运行,适配你的3D核数组可视化需求:

1. Matplotlib(基础3D可视化)

Matplotlib是Python最常用的可视化库,自带3D模块,能实现切片和等值面可视化,无需额外复杂依赖。

实现代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 定义核数组
kernel = np.array([[[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]],

             [[0.88369477, 0.9201748 , 0.5593715 ],
              [0.91746247, 0.9553365 , 0.5807462 ],
              [0.88369477, 0.9201748 , 0.5593715 ]],

             [[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]]])

# 1. 切片可视化
fig = plt.figure(figsize=(15,5))

# X轴切片(索引1,数组为3x3x3,索引范围0-2)
ax1 = fig.add_subplot(131, projection='3d')
x_slice = kernel[1, :, :]
X, Y = np.meshgrid(range(3), range(3))
ax1.plot_surface(X, Y, x_slice, cmap='viridis')
ax1.set_title('X轴切片 (index=1)')

# Y轴切片
ax2 = fig.add_subplot(132, projection='3d')
y_slice = kernel[:, 1, :]
X, Z = np.meshgrid(range(3), range(3))
ax2.plot_surface(X, y_slice, Z, cmap='viridis')
ax2.set_title('Y轴切片 (index=1)')

# Z轴切片
ax3 = fig.add_subplot(133, projection='3d')
z_slice = kernel[:, :, 1]
X, Y = np.meshgrid(range(3), range(3))
ax3.plot_surface(X, Y, z_slice, cmap='viridis')
ax3.set_title('Z轴切片 (index=1)')

plt.show()

# 2. 等值面可视化
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 生成网格坐标
x, y, z = np.mgrid[0:3, 0:3, 0:3]

# 绘制等值面(阈值设为0.8、0.9、0.95)
ax.contour3D(x, y, z, kernel, levels=[0.8, 0.9, 0.95], cmap='viridis')
ax.set_title('3D等值面')
plt.show()

2. Plotly(交互式3D可视化)

Plotly支持交互式操作(旋转、缩放、悬停查看数值),在Colab中渲染效果良好,适合探索性分析。

实现代码:

import numpy as np
import plotly.graph_objects as go

kernel = np.array([[[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]],

             [[0.88369477, 0.9201748 , 0.5593715 ],
              [0.91746247, 0.9553365 , 0.5807462 ],
              [0.88369477, 0.9201748 , 0.5593715 ]],

             [[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]]])

# 1. 切片可视化(X轴切片,其他轴同理)
x_slice = kernel[1, :, :]
X, Y = np.meshgrid(range(3), range(3))

fig = go.Figure(data=[go.Surface(z=x_slice, x=X, y=Y)])
fig.update_layout(title='X轴切片 (index=1)', autosize=False, width=600, height=500)
fig.show()

# 2. 3D等值面可视化
x, y, z = np.mgrid[0:3, 0:3, 0:3]
fig = go.Figure(data=go.Isosurface(
    x=x.flatten(), y=y.flatten(), z=z.flatten(),
    value=kernel.flatten(),
    isomin=0.8,
    isomax=0.95,
    colorscale='Viridis',
    surface_count=3, # 显示3个等值面
))
fig.update_layout(title='3D核数组等值面')
fig.show()

3. PyVista(专业3D科学可视化)

PyVista专注于3D网格和体数据可视化,API简洁,能轻松实现切片、等值面等操作,适合处理三维数值数组。

实现代码:

!pip install pyvista -q # Colab中需要先安装
import numpy as np
import pyvista as pv

kernel = np.array([[[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]],

             [[0.88369477, 0.9201748 , 0.5593715 ],
              [0.91746247, 0.9553365 , 0.5807462 ],
              [0.88369477, 0.9201748 , 0.5593715 ]],

             [[0.88121283, 0.91759044, 0.5578005 ],
              [0.91488576, 0.9526534 , 0.5791151 ],
              [0.88121283, 0.91759044, 0.5578005 ]]])

# 将numpy数组转为PyVista的体数据
grid = pv.UniformGrid()
grid.dimensions = np.array(kernel.shape) + 1
grid.spacing = (1, 1, 1)
grid.origin = (0, 0, 0)
grid.cell_data['values'] = kernel.flatten(order='F')

# 1. 多平面切片可视化
plotter = pv.Plotter()
plotter.add_mesh(grid.slice('x', origin=(1,1,1)), cmap='viridis')
plotter.add_mesh(grid.slice('y', origin=(1,1,1)), cmap='viridis')
plotter.add_mesh(grid.slice('z', origin=(1,1,1)), cmap='viridis')
plotter.show()

# 2. 等值面可视化
plotter = pv.Plotter()
plotter.add_mesh(grid.contour([0.8, 0.9, 0.95]), cmap='viridis')
plotter.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:57:00