Dot Physics

Plotting with VPython and PyLab

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.

i-deca1709b10d59ebd187af4db0f65f1b-vpython_1.jpg

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:

i-01ade5b38a36dd698ca542a2c5f5780a-spark-graph.jpg

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.

i-c9cf80cb3e2f3c542ff4f12a3ef87acc-pylab.jpg

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

i-6a9cc7aa4970b46305d6516433f1c725-key_line.jpg

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.

i-c36b4e3520b484b9b99cdcd2103cc7a9-figure_1.jpg

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:

i-83acdaf0fdc1f3b951de672ff7a928a3-figure_1_1.jpg

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

Comments

  1. #1 JYB
    July 30, 2009

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

  2. #2 Rhett
    July 30, 2009

    @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

  3. #3 Fran
    July 31, 2009

    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.

  4. #4 Rhett
    July 31, 2009

    @Fran,

    Gridlines – yes. Just add

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

    before the show()

The site is undergoing maintenance presently. Commenting has been disabled. Please check back later!