(alternate title: how to make pretty graphs in vpython)
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 – 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.
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.