# The beauty of Mayavi

Four charges with different magnitude plotted in 3D using Mayavi

In one of my earlier posts about Mayavi, I wrote about how you could visualize 2D field line plots using the flow function. At the end of that post I added that Mayavi is actually best at 3D plotting, and to follow up on that I’ll show you some of these plots with a few example Python scripts you might try out on your own.

First of all, you might want to know how to install Mayavi. For those lucky ones of you who have freed yourself and jumped on the Linux bandwagon, installing Mayavi should be quite easy. If you are using Ubuntu in particular, you may just install the package mayavi2 using either Synaptic or apt-get. If you are on Windows or Mac, you may either install Enthought’s own Python distribution (EPD) or give a shot at compiling on your own. Just note that EPD is quite expensive, even though all its components are open source, but if you are a student or academic user you could go ahead and download the academic version for free. It is basically the same as the commercial one, but with an academic license. (Kudos to Enthought for both making Mayavi open source, building an business model around it and still providing a great solution for students!)

### Now, Enter 3D!

The way you do your plots in Mayavi depends on what you want to express. Most likely, you would prefer to show some simple plots giving just the necessary amount of information to tell you how the electric field behaves around your charges. A simple example of this is shown below:

The simple plots often give you a great perspective about what happens in the electric field

On the other hand, you might want to give a strong visualization to show off the density and beauty of an electric field. In such a case, increasing the resolution of the flow/streamline seeds gives you a greater amount of field lines, which could result in plots like this:

An highger seed resolution gives a more dense plot.

Note that the plot shows the same four charges as above, but from a different angle and, of course, with more field lines.

The greatest part of using Mayavi to visualize these plots, however, comes from the fact that you may rotate the plot in real time in the Mayavi scene view. This gives you great control and insight of what you are plotting as you may rotate it as if it was a physical object in front of you. To show how interesting this may be, I’ve created a short video rotating around the same plot as above, recorded using Mayavi’s animation features:

You may even animate the charges individually, showing you what happens when a charge moves through space.

Note that the video below shows some artifacts around the charges. I believe I could have tweaked the settings a bit more to avoid these, but I decided it was good enough for the purpose of showing Mayavi’s capabilities.

If you would like to test out these plots on your own, you can download the source code here:

And if you couldn’t get enough of those field line plots, here are all the above and a some more, stacked in one set:

## 15 thoughts on “The beauty of Mayavi”

1. When fieldlines can be described by the word ‘sexy’, you know you’ve done a good job with your visualization

2. I was simply amazed with the way you guys presented 2D and 3D designs. Unfortunately, I have not yet jumped on the Linux bandwagon as of yet. But with this new concept, I might turn over a new leaf. Thank you for sharing this.

3. Thanks, Inah!

As mentioned earlier, you don’t have to use Ubuntu or Linux to create these designs, but it certainly is easier to get the necessary tools and packages downloaded right away using Ubuntu.

5. @pratik: Take a look at the Mayavi documentation. The examples should give an idea about what Mayavi is capable of and how to get started. We will probably write a couple more tutorials about Mayavi here as well, so make sure you check back later.

6. also, wouldn’t it be a better idea to just compute the scalar potential and use the mayavi filters to get the derivatives (i.e vector field)? Perhaps this would be more general :). I did just that for the dipole configuration, now I am working it out for other systems too.

7. @pratik: That is no bad idea. I guess it depends on what you want to do with your data in the end. We started out with some Matlab examples and worked our way from there, so we ended up using the vector field directly.

I didn’t really think about that you could use Mayavi’s filters to get the derivatives. Thanks for the tip!

8. how can i make a movie with this code

from mayavi import mlab
import numpy as np
import random
x=[1]
y=[1]
z=[1]
c=(0,0,0)
obj = mlab.points3d(x,y,z,color=c,scale_factor=1,opacity=0.7)
mlab.draw()
for t in range(100):
a=random.uniform(0,1)
b=random.uniform(0,1)
d=random.uniform(0,1)
print t,a,b,d
c=(a,b,d)
#print obj
mlab.points3d(x,y,z,color=c,scale_factor=1,opacity=0.7)
#obj.color=c
#for t in range(1, 10):
mlab.draw()

9. Hello,

I’m just wondering is there any way to export such animations to Blender?

• Yes. The savefig function has the possibility to export to .obj files, among other formats. The .obj files may be opened in Blender to create an animation.

http://docs.enthought.com/mayavi/mayavi/auto/mlab_figure.html#savefig

I don’t know if there is a quick way to load one .obj file per frame in Blender, but I think it should be fairly easy to create a script for that.

10. Hi,

It’s very nice to come across such beautiful images and videos. I am working on a problem where my 3D plot need to change color with time. Just like the one you have shown in the second video of this post. I know that we can use color legend on the side of the plot but I don’t know how to play recorded vectors containing voltage values which were collected during a simulation run on another platform.

Is there a way to do that? I will explain in more detailed way. Suppose I have 100 points on the 3D plot which are circular shape. Now, I have recorded vectors for all these 100 points which are collected from another platform. The data inside these vectors are voltage values starting from negative values and reach positive peak and then return back to negative values with respect to time. So, my points should change color from blue to red depending on the same time.

Is there a way to do this? Kindly let me know.

Thank you.

• I think the points3d function in mayavi could work out for you.

```from mayavi.mlab import *
t = numpy.linspace(0, 4*numpy.pi, 20)
cos = numpy.cos
sin = numpy.sin

x = sin(2*t)
y = cos(t)
z = cos(2*t)
s = 2+sin(t)

mypoints = points3d(x, y, z, s)
for i in range(30):
s = 2 + sin(t + i * 0.1)
mypoints.mlab_source.set(scalars=s)
```

This should animate the size and color of the points as the values in the s array changes in the for loop.

• Hi,

Thank you so much. It is indeed very helpful. I need to try this on my code. However I have another question, I have come across some fluid dynamics simulations in Mayavi, which showed movement with respect to time. How is that kind of simulation done in Mayavi?

If there were examples related to that, could you kindly let me know.

Thank you very much.

• It is pretty much the same type of code as in the example above, but you will have to use surf instead of points3d.