James PK's Technical Journal

[ Home | RSS 2.0 | ATOM 1.0 ]


Sat, 10 Mar 2012

Basic Graphs with Matplot Lib

"Understanding Algorithms and Data Structures" by Brunskill and Turner was on the reading list of one my units on my CS course.

I enjoy doing the exercises. Page 21 (in the chapter "Necessary Mathematics") has an exercise on sketching and comparing graphs for various mathematical functions. I thought I'd have a go I'd at it with Python and Matplotlib (a Python library for graphs). Using a combination of range, map (which applies a function to every item of an iterable), determining the function name (via .__name__), which are passed to Matplolib, the code is fairly succinct, as seen in this code snippet:


for i in functions:
 plt.plot(range(5), map(i,range(1,6)), 'o-', label=i.__name__)
 print i.__name__,map(i,range(1,6)) 

Image output:

Full source doe is ./code

posted at: 15:13 | path: / | permanent link to this entry

Powered by PyBlosxom