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 am not a programmer. Just to be clear. I use python to get things done, but I am sure it could be done in more efficient ways. Anyway, I sure you know how much I like vpython - especially for teaching physics. However, sometimes I use it for blogging stuff also. The problem is that vpython…
Numerical calculations are grand. Of course, they are just another form of normal calculations - like I have said before. So, I have mostly used [VPython](http://vpython.org) for my calculations on my posts. But there is a new game in town: [Easy Java Simulations](http://www.um.es/fem/Ejs/).…
Looking back at part I of this idea, I don't think I did a very good job. Let me summarize the key things I wanted to say: Normally, there are two ways of modeling the motion of an object: Calculating the forces on the object and using the momentum principle or Newton's second law (which are the…
Maybe you know I like numerical calculations, well I do. I think they are swell. [VPython](http://vpython.org) is my tool of choice. In the post [Basics: Numerical Calculations](http://scienceblogs.com/dotphysics/2008/10/basics-numerical-calculation…) I used vpython and excel to do something…

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