3D Plotting in Python with matplotlib mplot3d

I recently needed to do some simple 3D plotting in python. The strongest choice as discussed on the web is Mayavi, which is part of the Enthought Python distribution. However, I have a previous python distribution already installed (via scisoft) with all of my favorite packages updated to the latest and greatest versions (e.g. matplotlib, PyEphem, slalib for python, etc.). I wanted to install Mayavi into my existing pythong distribution. In theory this should have been straightforward as there are eggs (a python package zip-type file). However, no matter how hard I tried, I could not get Mayavi, and all its required software, installed properly. Part of this may have been that I was doing a local install without admin privileges.

After spending an entire day with Mayavi issues, just in my attempt to make a very simple 3D scatter plot, I ran across a much easier option. The latest edition of matplotlib (0.99.1) comes with a toolkit called mplot3d. After looking over examples here and here, it took me about 5 minutes to download and install the latest matplotlib version and I was ready to go! Simple 3D figures are a snap to put together and you can interact in order to zoom and rotate around. The toolkit uses matplotlib on the backend, so if you are familiar with all of the plotting options there, then mplot3d follows on logically.

My plot from matplotlib.mplot3d

My plot from matplotlib.mplot3d

There are still a few bugs/features that I haven’t quite figured out. Most notably, in wire or surface plots, I can’t seem to set the rstride and cstride (row and column segment step sizes) to anything less than 1. But one of my favorite things about python is that I should be able to go into the source code for mplot3d and figure out what the issue is.

If you have experience with either Mayavi or mplot3d, let us know in the comments when you would need to use Mayavi over the simpler functionality of mplot3d or if you have run into other issues with either.

7 comments… add one
  • Casey Law Feb 1, 2010 @ 17:09

    Thanks for the post! I also struggled with Mayavi installation and eventually gave up on my hopes for 3d python plots. Now I’ve reproduced a few of the demos with matplotlib 0.99.

    So glad I am subscribing to this blog!

  • Michael Fitzgerald Feb 3, 2010 @ 4:45

    I didn’t encounter problems installing mayavi using easy_install, which also lets one use a custom installation prefix (e.g. easy_install-2.6 –prefix=$home Mayavi). I haven’t yet tried mplot3d — thanks for the tip.

  • Matt Feb 19, 2010 @ 2:27

    I’ve used Mayavi and found it to be very nice as an integrated environment, but I ended up using the underpinning library, TVTK, instead. mplot3d is great for prepared visualizations of relatively simply data, but for exploratory viz (like in simulations) it isn’t necessarily suitable. But by dropping down to TVTK, much more control over the data sources and mechanisms are available — including things like hierarchical (i.e., AMR) data sources. Plus, all the awesome widgets in Mayavi2, like for colormaps and labels, are still available!

    As for the installation, I’ve had really good luck installing VTK on its own, installing the ETSProjectTools and using the “ets co” “ets develop” commands to conduct a source installation, as mentioned on the Enthought website. This keeps most of the setuptools (forked as distribute) toolchain out of the way. For deploying software on heterogeneous machines without admin privs, fully-isolated source installations were the only solution we could come up with for astro-viz packages, and most of the binary eggs (even things like mercurial!) were problematic.

  • Peter S May 7, 2011 @ 1:07

    This blog article is quite old now, but getting these things going on macs is an ongoing issue. My thoughts are with anyone who aspires to getting the ultimate astropython workbench up and running on mac os x.

    I am current wrestling with my whole mac os x python/matplotlib/mayavi setup. I have a linux box at work and the ease with which I can set up all these things compared to this whole mac os x bunfight makes me want to trade in my macbook pro for a linux laptop—well, not quite.

    I have had a working installation of matplotlib on my macbook which brings me much joy. I also have astLib and kapteyn and all sorts of other goodies that have gradually become my astropythonic friends. But now I want to get mayavi going in conjunction with matplotlib, and despite a year or two of fiddling with paths and thinking that eventually I would get to know the anatomy of my mac 3rd party software setup, I just can’t seem to get it going. I just want to make a nice 3d model of the local universe a la Tully et al (2008) that I can superimpose peculiar velocity vectors on. Can that be too much to ask? It seems so. This mplot3d is a good suggestion, but I really like the way vtk depicts all these things on my work computer—I am a deeply superficial person. The enthought thing has never worked for me, and I have spent days (and nights) trying to set up vtk with python wrapping on my laptop to my satisfaction, but no joy.

    I will keep trying with mayavi/vtk with matplotlib on mac and let you know if I find a magic bullet…

  • Tom May 7, 2011 @ 9:38

    @Peter – I’m surprised EPD doesn’t work for you, and I think that the key will be to get that working (rather than trying to build from source). With EPD you should be able to get a fully working Python installation including numpy/scipy/matplotlib/mayavi/etc. in about 10 minutes, and it takes another 10 minutes to get many astro modules installed. Check out this page we made for a Python workshop at the CfA for installation instructions. Install the 32-bit EPD package from here because the 64-bit version doesn’t come with Mayavi (yet). What are the issues you run into when you do this?

    EDITED: the 64-bit version of EPD does *not* come with Mayavi, so you need to install the 32-bit version

  • Peter S May 8, 2011 @ 3:28

    @Tom: I had installed the 64-bit version of EPD. I removed this and replaced it with the 32-bit version and re-installed the astro modules, and now I’m ready to go. Fantastic! Thanks!

  • George Apr 5, 2014 @ 19:27

    FWIW I installed Canopy Enthought and Mayavi came with the installation. The whole installation took about 5 minutes. Painless. I played with the stand alone Mayavi GUI – everything works.
    Hope this helps. Windows 7, 64 bit

Leave a Reply

Your email address will not be published. Required fields are marked *