Now why would you want to do that? Well, you could want to run a script on a remote machine from a (non graphical) terminal and just save the figure to a file, which you then retrieve. In my case I was making animations of simulations and wanted to do the animation on a remote machine, stitch the images together using mencoder and just retrieve the resultant movie file. At the top of the file add the lines
import matplotlib as mp
This causes the Agg backend to be used.
Thanks to Christopher Barker of NOAA for this.