Now on ScienceBlogs: HeartlandGate: Anti-Science Institute's Insider Reveals Secrets

ScienceBlogs Book Club: Inside the Outbreaks
dotphysbanner.jpg

Dot Physics

What happens when you take some basic, introductory physics and apply them to cool things you see? Dot Physics happens. This blog looks at movies, experiments, demos and other topics typically aimed at the introductory physics level.

Profile

allain_pic4.jpg Rhett Allain is an Associate Professor of Physics at Southeastern Louisiana University. He enjoys teaching and talking about physics. Sometimes he takes things apart and can't put them back together.

Search

Recent Posts

Recent Comments

Archives

Blogroll

« Allegory of the Grade | Main | "I hate math" »

Visual and MatplotLib - holding hands at last

Category: accelerationgraphnumericalphysicsprojectile motionpython
Posted on: July 19, 2010 12:28 PM, by Rhett Allain

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

Tett.py     Tett.py

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

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.

Share on Facebook
Share on StumbleUpon
Share on Facebook
Find more posts in: Physical ScienceTechnology

TrackBacks

TrackBack URL for this entry: http://scienceblogs.com/mt/pings/143994

Comments

1

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

Posted by: Andrew York | July 19, 2010 2:58 PM

2

Why not use Gnuplot? http://www.gnuplot.info/ I assume it's a little easier than both vPython and Mathplotlib.

Posted by: Andrew Belt | August 8, 2010 11:01 AM

Post a Comment

(Email is required for authentication purposes only. On some blogs, comments are moderated for spam, so your comment may not appear immediately.)





ScienceBlogs

Search ScienceBlogs:

Go to:

Advertisement
Follow ScienceBlogs on Twitter

© 2006-2011 ScienceBlogs LLC. ScienceBlogs is a registered trademark of ScienceBlogs LLC. All rights reserved.