8

In Mayavi I would like to see the grid of the axes in the following plot

# 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://stackoverflow.com/a/32531283/2729627>>.
mlab.orientation_axes()  # Source: <<https://stackoverflow.com/a/26036154/2729627>>.
mlab.show()

like in Matlab

The axes grid is shown as I want here

Which command do I have to employ in Mayavi to achieve this?

Adriaan
  • 715
  • 10
  • 22
  • did you manage to create such axis? If yes, how? – Tiago Martins Peres Nov 02 '19 at 09:47
  • 1
    @TiagosupportsGoFundMonica It's been a while, but as far as I can remember I gave up because Mayavi keeps 'complaining' with errors for even the simplest tasks, like showing a grid. – Adriaan Nov 04 '19 at 09:31
  • thank you. The axis is easy to show but the grid... Their focus is more about showing as many points as possible – Tiago Martins Peres Nov 04 '19 at 09:57
  • did you use something else instead @Adriaan ? – Tiago Martins Peres Nov 04 '19 at 10:03
  • 1
    @TiagosupportsGoFundMonica I'm still 'stuck' with Matplotlib which has many capabilities, also for 3d, and is easy to work with, but it is unfortunately EXTREMELY SLOW. This is well known disadvantage of Matplotlib. I didn't find an alternative yet. – Adriaan Nov 04 '19 at 13:06
  • I've seen examples of 3D bar charts built with Matplotlib and they were soo imperfect rendering. Then, [found out in their FAQs they also suggest Mayavi](https://matplotlib.org/mpl_toolkits/mplot3d/faq.html#how-is-mplot3d-different-from-mayavi). Know of some others due to [this answer](https://stackoverflow.com/a/55967461/5675325). – Tiago Martins Peres Nov 04 '19 at 13:18

1 Answers1

1

My suggestion would be to use a user defined filter like CubeAxesActor from VTK (which seems more complete than the Axes filter in default Mayavi). Which in Mayavi would translate into something like

...
from mayavi import mlab
from tvtk.api import tvtk

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

But it seems Mayavi does not like this filter

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

I'm not sure if it is related to my VTK version but you might try

PerroNoob
  • 843
  • 2
  • 16
  • 36
  • How would you employ this 'filter' in my code example to get the grid to show for all the three axes? – Adriaan Feb 26 '19 at 08:42
  • After `surf=...` you can add `mlab.pipeline.user_defined(surf, filter=tvtk.CubeAxesActor())` – PerroNoob Feb 27 '19 at 11:18
  • I add the lines `import tvtk` and `mlab.pipeline.user_defined(surf, filter=tvtk.CubeAxesActor())` to the code after the `surf` command, but unfortunately get the error `AttributeError: module 'tvtk' has no attribute 'CubeAxesActor'`. – Adriaan Mar 04 '19 at 11:03
  • You have to import `tvtk` as `from tvtk.api import tvtk` – PerroNoob Mar 04 '19 at 12:53
  • 2
    I unfortunately get the error `File "...\lib\site-packages\mayavi\filters\filter_base.py", line 73, in update_pipeline fil.update() AttributeError: 'CubeAxesActor' object has no attribute 'update'` – Adriaan Mar 05 '19 at 13:26