Basic graphing with MatPlotLib
One of the Python modules that has most interested me recently is MatPlotLib which is a sophisticated graphing module which can be used to create journal grade graphs of almost anything. The official gallery for MatPlotLib is worth checking out to get an idea of the sheer range of graph types it can be used to create.
It is simple enough to get started using MatPlotLib for example to create a line graph of x*x and save it as a PNG file requires only the following:
"""Simple demonstration of MatPlotLib plotting.""" from matplotlib import pyplot X = range(0,100) Y = [ i*i for i in X ] pyplot.plot( X, Y, '-' ) pyplot.title( 'Plotting x*x' ) pyplot.xlabel( 'X Axis' ) pyplot.ylabel( 'Y Axis' ) pyplot.savefig( 'Simple.png' ) pyplot.show()
The above script will produce the following graph:

To plot data over a time period the simplest solution is to convert date/time units to timestamps using MatPlotLibs date2num function and then to plot using the plot_date method as follows:
"""Simple demonstration of MatPlotLib Date plotting.""" from matplotlib import pyplot from matplotlib.dates import date2num from datetime import datetime, timedelta # Generate a series of timestamps from today to today + 100 years. X = [date2num(datetime.today()+timedelta(days=365*x)) for x in range(0,100)] Y = [i*i for i in range(0,100)] pyplot.plot_date( X, Y, '-', xdate=True ) pyplot.title( 'Plotting x*x' ) pyplot.xlabel( 'X Axis' ) pyplot.ylabel( 'Y Axis' ) pyplot.savefig( 'SimpleDates.png' ) pyplot.show()
Which will generate a chart like the following:

As you can see it is fairly simple to graph data using MatPlotLib. This makes Python and MatPlotLib a compelling solution for data analysis when combined with the many available modules for dealing with common data storage formats like text (using RegEx), CSV, XML and JSON files and SQL databases.
Why work doesn’t happen at work
This talk is by Jason Fried co-founder of 37 Signals talking at TED MidWest 2010 about the hurdles ofgetting work done in the average office. Jason makes some good points about how working in offices can be counter productive due to interruptions and unnecessary meetings.
I’ve noticed a trend over the years that the most productive individuals in the office tend to either arrive before everyone else or stay late after everyone else leaves to get quiet interruption free time to get stuff done. I’ve also yet to meet a programmer who was more productive in an high interruption environment.
I know of at least one development team that has banned meetings on certain days of the week for their engineers and before I started working at home I used to block off time in my calender to prevent my days being fragmented by meetings.
Working at home I find I have the opposite problem and I need to set alarms to remind me to take breaks…
Praise for Python
- Simplified Memory management
I am so much more productive when I am not having to worry about pointer related errors e.g. pointer math or sweat the subtleties of memory management e.g. memory alignment while writing code. - Less structural syntax
After using Python for a while I really appreciate it’s use of indentation to give a program structure, as it makes python source code much more concise than C/C++. - No compiling or linking
It is so much easier to stay in the flow when your not waiting 5-30 minutes for compilation and linking. I’ve recently taken to running PyLint when I miss the feedback from a compiler/linker on my program structure and to learn the coding style outlined in the Python Style Guide. - Selective imports
Having worked on large scale C/C++ projects for most of my career I really appreciate the ability to only import what I want from modules and the option to also rename (or alias) what I’ve imported. - Batteries included philosophy
The sheer scope of the library of modules included in Python means I can spend more time writing the interesting parts of my programs, as most of the time the utility functionality I need is just an import away. - Package management
The Python Package Index (PyPi) and Setup Tools module make installing most python modules as simple as ‘easy_install <module_name>’. - Duck typing
Python’s use of Duck Typing emphasizes interfaces over types which makes it so much easier to supply my own classes to standard library functions, as I only have to implement as much of the interface as is required.









