3D Plotting in Python with matplotlib mplot3d

by Jessica on February 1, 2010

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.

Share This:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Twitter
  • PDF

{ 3 comments… read them below or add one }

1 Casey Law February 1, 2010 at 5:09 pm

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!

2 Michael Fitzgerald February 3, 2010 at 4:45 am

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.

3 Matt February 19, 2010 at 2:27 am

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.

Leave a Comment

Previous post:

Next post: