Python

I use Python and Matplotlib (a plotting module for Python) as part of my day job. I often produce fairly complicated plots that take a bit of fiddling with to get right, so I wanted to make some notes about how I’ve done various things (e.g. set the font for xticklabels, colorbar tick labels etc.) in case I forget.

I use Python on a 2008 MacBook Pro, running Max OS X 10.5.8, Python 2.7 and matplotlib.


Mac OS X ships with Python 2.5, including a few useful modules such as Numpy. If you want to add more modules, then the fun begins as Python 2.5 isn’t stored in the ‘usual’ place – when I tried to install matplotlib, for example, I would get a ‘system requires python.org system Python’ error. We got round this by installing Python 2.7, which comes with a nifty little script called ‘Update Shell Profile.command’. If you run this script, Python 2.7 becomes the system version of Python and it becomes possible to install modules.


Setting the font for text on the figure
This is best done using matplotlib rc

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.arange(0,11,1)
y = x*2

mpl.rc(‘font’, family=’serif’, size=10) #sets the text family to serif and size to 10pt
plt.plot(x,y)
plt.show()

This sets the text parameters throughout the figure.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s