Visual and MatplotLib - holding hands at last

(alternate title: how to make pretty graphs in vpython)

I am happy. Finally, I can use the visual module in python (vPython.org) and plotting with Matplotlib. Maybe this isn't such a big deal for many of you, but for me, it never worked until now.

In the past, I blogged about plotting in vpython vs. matplotlib. My conclusion was that it was easier in vpython, but prettier in matplotlib. So, why not just use matplotlib? There are a couple of things that make vypthon very attractive.

  • Vectors. Vpython has a built in vector class (or function - I don't know what I am talking about). There are also things like dot-product, magnitude, normal vector. With matplotlib, I would have to use arrays to represent vectors. It is just a little more clumsy.
  • Objects. In vpython, I can make an object ball=sphere(). Then I can do stuff like calling the mass ball.m and the momentum ball.p.
  • 3d. If you want to make a visual representation of what is happening - basically you are there.

If you look back at one of my zombie posts, I actually did the calculation twice. First I did it in vpython, then I redid it with matplotlib to make a pretty graph. Now, I don't have to do that.

Setup

First, what was the problem. I found out from the visualpython-users mailing group that the problem was with my installation of matplotlib. I had used the Enthought python distribution and this was the problem. Vpython and Enthought were doing things different with the installation. Let me just leave it at that.

To fix this, I manually installed matplotlib (apparently only the older version works - 0.99.1.1). Boom. That did it. Now I can load both visual and pylab modules at the same time. If you are using a Windows computer, your fix might be easier (I am on Mac OS X). Reports say the pythonxy project has all this stuff together already.

There is still one problem. If you try to run a visual display window and a matplotlib graph at the same time, bad things can happen. Sort of like crossing the streams in Ghostbusters.

Making graphs

Let me go back to the same example I used when talking about matplotlib vs. vpython, but I will make some modifications. Here is my program:

i-cbfcfefae96d30100a681629295fc9fd-2010-07-19_tettpy_tettpy.jpg

And here is the pretty output. Shiny, right? Well, way better than the normal vpython graphs (sorry vpython, but it is true)

i-e70552f4c4ae71b59378b4b0a9203648-2010-07-19_matplotlib_out.jpg

If you want to go back to vpython output, just comment out the scence2.visible=false line. Also comment out all the matplotlib stuff at the end. Boom, there it works again.

Let me point out one final difference between plotting in matplotlib and vpython. In matplotlib, you have to collect all the data and then plot it. In vpython, you can plot one data point at a time as you calculate this stuff. The nice thing about the vpython way is that you can see how the plot develops over time.

More like this

I believe you can plot data points one at a time in matplotlib. I assume my tabbing will be mangled, but run this from within ipython:

import pylab

pylab.figure()
for a in range(10):
(tab)b = a**2
(tab)pylab.plot([a], [b], '.')
(tab)pylab.show()
(tab)print "Hit enter to continue..."
(tab)raw_input()

By Andrew York (not verified) on 19 Jul 2010 #permalink