How to post Python source code on WordPress

I’m back to posting now that I am on sabbatical; things have evolved on WordPress in the last year, and now it’s easy to post source code. See the following link so that you can see read about how easy it is to include code in WordPress.

Of course, if you are editing a post on WordPress, you should see the little “code” textual icon in the editing toolbar; just click it and you can insert exciting code:

import numpy as np
import matplotlib.pyplot as plt

t = linspace(0,10,200)
plot( t, sin(t) )
show()
Posted in Interesting Links, Matplotlib, Plotting | Tagged , , | Leave a comment

Miscellaneous Python Links

Here’s a nice link to a pdf file with a summary of matlab, NumPy, and R commands.Very handy for people like me that cannot remember all the various commands one needs to get something done.

Also, if you haven’t tried out the new iPython Notebook, you should give it a try; it can mix markup (i.e. text–and LaTeX mathematics!) and python code and output in mathematica style. It’s wonderful for interactive work, development, and making instructional demonstrations. You’ll need to install iPython 0.12 or later, available here.

Here’s a very handy python package: the uncertainties package. If you have setuptools installed, you can install it with

sudo easy_install --upgrade uncertainties

See the web page for more info; if you’ve ever spent 10 minutes taking a boatload of partial derivatives (and if your an experimental physicist, you have)  to propagate errors, this package is an amazing time saver.

A project I’m keeping my eye on is PANDAS, which is a Python Data Analysis Library. It was created by someone working in the financial world (Wes McKinney); so I’m not clear yet that it’s the right tool for physicists, but it’s looking interesting. Here is a video introduction by the author himself.

 

 

 

Posted in data analysis, error propagation, Interesting Links, iPython, Pandas | Tagged , , , , | Leave a comment

Simple Animated Plot with Matplotlib

Here’s a simple script which is a good starting point for animating a plot using matplotlib’s animation package (which, by their own admission, is really in a beta status as of matplotlib 1.1.0). I find the code needed to perform the animation more cumbersome than I’d like, but importantly, it’s not too cumbersome. In line2 50-51 of the code, where the actual animation call is made, note that

animation.FuncAnimation(fig, simPoints, simData, blit=False,
   interval=10, repeat=True)

has the user function simData as the argument for the user function simPoints. These are two functions that the user has to provide. A little staring at the code, and I think you’ll quickly be able to adapt it to suit your needs.

I don’t think of this as a great example how I eventually want to construct this site, but I think I realize that I should at least start somewhere, and perhaps when I am on sabattical and have more time to devote to this, I’ll nail down some nice structures for archiving, searching and displaying code. For now, I’ll archive the code on PasteBin  (link: SimpleAnimatedPlot.py ), and provide a local copy here (double clicking on the code selects it all):

"""
This short code snippet utilizes the new animation package in
matplotlib 1.1.0; it's the shortest snippet that I know of that can
produce an animated plot in python. I'm still hoping that the
animate package's syntax can be simplified further. 
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def simData():
# this function is called as the argument for
# the simPoints function. This function contains
# (or defines) and iterator---a device that computes
# a value, passes it back to the main program, and then
# returns to exactly where it left off in the function upon the
# next call. I believe that one has to use this method to animate
# a function using the matplotlib animation package.
#
    t_max = 10.0
    dt = 0.05
    x = 0.0
    t = 0.0
    while t < t_max:
        x = np.sin(np.pi*t)
        t = t + dt
        yield x, t

def simPoints(simData):
    x, t = simData[0], simData[1]
    time_text.set_text(time_template%(t))
    line.set_data(t, x)
    return line, time_text

##
##   set up figure for plotting:
##
fig = plt.figure()
ax = fig.add_subplot(111)
# I'm still unfamiliar with the following line of code:
line, = ax.plot([], [], 'bo', ms=10)
ax.set_ylim(-1, 1)
ax.set_xlim(0, 10)
##
time_template = 'Time = %.1f s'    # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
## Now call the animation package: (simData is the user function
## serving as the argument for simPoints):
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
     interval=10, repeat=True)
plt.show()
Posted in Animation, Matplotlib, Plotting | Tagged , , , , | 11 Comments

First Post

This is the beginning of the Scientific Python Script Repository.
My intention is to create an
(a) open-source,
(b) high quality & elegant,
(c) searchable
database of  python scripts (from snippets to more
extended projects) useful for scientists aiming to get things done
with python.

To do this, I’ll have to make strict standards for code,
verification of code, and documentation. This is no small task;
so if you’re a scientist with expertise in python let me know.

You can contact me through my university home page:
http://people.usm.maine.edu/~pauln/
No accounts will be created without verification.

More to come over the next few months.

Posted in Introduction | 2 Comments