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

« Video Camera Options for video analysis | Main | Pluto. Planet, or not? »

Plotting with VPython and PyLab

Category: graphphysicspython
Posted on: July 29, 2009 10:52 PM, by Rhett Allain

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 doesn't make pretty graphs. Oh, they are quick and simple - but sometimes you want pretty also. Well, what if you just don't use vpython? Of course then I could use some other plotting package like pylab (which actually uses something else like matplotlib or something - I get confused). Or, I could use them together. Well, for me, these two don't get along (I think it is because pyplot uses python 2.6 and vpython is on 2.5 - maybe?). The one thing I miss in vpython is built in vector classes.

So, in this post, I am going to show simple projectile motion plotting with both vpython and pylab. This is also so that I will remember how to do this (I so often forget).

Vpython

Vpython is awesome. Ok, now that is out of the way, what am I going to do? I am going to model a ball thrown with an initial speed at a certain angle above the horizontal. I will make a plot of the x and y position as a function of time. How will I do this? I have talked about numerical calculations before, so check this out if you are interested.

Here is my vpython program.

Vpython 1

A couple of notes:

  • First, I didn't have to call the sphere() function. When I do that, vpython also makes a 3D object that I am going to ignore. The nice thing about using sphere() is that I can make the ball a sphere and use ball.pos.x for the x position and stuff.
  • There are options for the graph in vpython (like changing the background color and stuff) - but I left the defaults on.

And here is the output:

Graph

You can add labels to the axes and stuff, but I left that off for now.

Pylab

First, how do you get this pylab stuff? The ways are numerous. However, I would recommend the Enthought Python Distribution which is free for educational users - yay! This basically installs all the stuff you need and more. The biggest loss in not using vpython is the built in vectors. To get around this, I am going to use the vectors as arrays. Let me go ahead and show this program.

Pylab

What is different? Notice the arrays instead of vectors. The other big difference is that I don't plot each point as I calculate it. Instead I add it to a list and then plot that list. I don't know why you have to do it this way, but sometimes it acts funny if you try to plot it one point at a time.

The key plotting line is

Key Line

Here I am saying plot tp (time) vs. yp (vertical position). Also notice how I reference the y-component of position. In vpython, this was ball.pos.y. For this method, the y-component is the second value in the array (which starts at zero). So, that would be ball[1]. Finally, the show() function shows the graph. In this case, I also added axes labels and a title. Here is what it looks like.

Figure 1

The other nice thing (other than shiny graphs) is that pyplot actually lets you interact with the graph. At the top of the plot window, you see this:

Figure 1 1

You can zoom, move and even save the graph as an image.

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/129362

Comments

1

I've been wanting to learn vpython for awhile but like everything else, never get around to it. Any good resources for learning?

Posted by: JYB | July 30, 2009 12:24 AM

2

@JYB,

I think the best way to get into vpython is to work through the labs that go with the Matter and Interactions textbook. They are free - you can download the pdfs here.

http://www.compadre.org/PSRC/items/detail.cfm?ID=5692

Posted by: Rhett | July 30, 2009 8:46 AM

3

hmmm...have you talked with Bruce Sherwood about using vpython with pylab? I like the look of the pylab graph, I would like grid lines, though. I assume you can do that.

Posted by: Fran | July 31, 2009 7:39 PM

4

@Fran,

Gridlines - yes. Just add

grid(color='b', linestyle='-', linewidth=0.5)

before the show()

Posted by: Rhett | July 31, 2009 11:35 PM

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.