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 ( 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.


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 - 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:


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


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…
Yes, I love python. However, I am no expert. Most of the stuff I write in python is dirty and ugly code. Sometimes I figure out new things (things that programmers already know) and sometimes I forget these things. So, here are a few tips and tricks that I use from time to time. Really, I am…
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]( for my calculations on my posts. But there is a new game in town: [Easy Java Simulations](…
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…

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

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

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