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


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:


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


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.


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


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.


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:


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


  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


    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.

  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


    Gridlines – yes. Just add

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

    before the show()

New comments have been temporarily disabled. Please check back soon.