Mayavi如何显示坐标轴网格

8
在Mayavi中,我想在下面的图表中看到坐标轴的网格。
# Source: <<https://scicomp.stackexchange.com/a/23148/10048>>.
import numpy as np
from mayavi import mlab

# Test data: Matlab `peaks()`
x, y = np.mgrid[-3:3:50j, -3:3:50j]
z = 3*(1 - x)**2 * np.exp(-x**2 - (y + 1)**2) \
   - 10*(x/5 - x**3 - y**5)*np.exp(-x**2 - y**2) \
   - 1./3*np.exp(-(x + 1)**2 - y**2) 

mlab.figure(bgcolor=(1, 1, 1))  # Make background white.
surf = mlab.surf(x, y, z, colormap='RdYlBu', warp_scale=0.3, representation='wireframe', line_width=0.5)
mlab.outline(color=(0, 0, 0))
axes = mlab.axes(color=(0, 0, 0), nb_labels=5)
axes.title_text_property.color = (0.0, 0.0, 0.0)
axes.title_text_property.font_family = 'times'
axes.label_text_property.color = (0.0, 0.0, 0.0)
axes.label_text_property.font_family = 'times'
# mlab.savefig("vector_plot_in_3d.pdf")
mlab.gcf().scene.parallel_projection = True  # Source: <<https://dev59.com/Ao_ea4cB1Zd3GeqPIgAo#32531283>>.
mlab.orientation_axes()  # Source: <<https://dev59.com/I4Pba4cB1Zd3GeqPv7ev#26036154>>.
mlab.show()

就像在Matlab中一样

这里显示的轴网格正是我想要的样子

我需要在Mayavi中使用哪个命令才能达到这个效果?


你成功创建了这样的轴吗?如果是,怎么做的? - Tiago Martins Peres
1
@TiagosupportsGoFundMonica 好久不见了,但我还记得我放弃了,因为Mayavi即使是最简单的任务,比如显示网格,也会出现错误和“抱怨”。 - Adriaan
谢谢。坐标轴很容易显示,但网格线有点困难……他们更注重尽可能多地显示点。 - Tiago Martins Peres
你用了其他的东西吗,@Adriaan? - Tiago Martins Peres
1
@TiagosupportsGoFundMonica 我仍然在使用Matplotlib,它有许多功能,包括3D,而且易于使用,但不幸的是它非常慢。这是Matplotlib已知的缺点。我还没有找到替代品。 - Adriaan
我曾经看到用Matplotlib构建的3D条形图的例子,但它们的渲染非常不完美。后来,在他们的FAQ中发现他们也建议使用Mayavi。根据这个答案,我知道还有其他一些选择。 - Tiago Martins Peres
1个回答

1
我的建议是使用用户定义的过滤器,例如来自VTK的CubeAxesActor(似乎比默认的Mayavi中的Axes过滤器更完整)。在Mayavi中,这将转化为以下内容:
...
from mayavi import mlab
from tvtk.api import tvtk

...
# Load data
...
mlab.pipeline.user_defined(data, filter=tvtk.CubeAxesActor())
...

但看起来Mayavi不喜欢这个过滤器

traits.trait_errors.TraitError: Cannot set the undefined 'input_connection' attribute of a 'CubeAxesActor' object.

我不确定这是否与我的VTK版本有关,但你可以尝试一下


你会如何在我的代码示例中使用这个“filter”来使得网格显示在所有三个轴上? - Adriaan
surf=... 之后,您可以添加 mlab.pipeline.user_defined(surf, filter=tvtk.CubeAxesActor()) - PerroNoob
我在 surf 命令之后添加了 import tvtkmlab.pipeline.user_defined(surf, filter=tvtk.CubeAxesActor()) 这两行代码,但不幸的是出现了错误 AttributeError: module 'tvtk' has no attribute 'CubeAxesActor' - Adriaan
你需要将 tvtk 导入为 from tvtk.api import tvtk - PerroNoob
2
很不幸,我遇到了错误 File "...\lib\site-packages\mayavi\filters\filter_base.py", line 73, in update_pipeline fil.update() AttributeError: 'CubeAxesActor' object has no attribute 'update' - Adriaan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接