Overplotting with matplotlib

For the [ALMA SSE] I had to code up some overlays using matplotlib. This was so visitors to our stand at the exhibit could take away a printed copy of themselves as “seen through ALMA’s eyes” – and this involved some simple overlaying with maplotlib. Now, given I tend to forget useful things like this I thought I’d post it here and here is an example of the output:
ALMA SSE output image
It turns out that a combination of the python packages numpy, imager and matplotlib is a really quick way to do the overlaying:

import matplotlib.pyplot as plt
import Image
import numpy as np
im = Image.open('logo.png')
height = im.size[1]
# Need array of floats between 0-1, not a uint8 array between 0-255
im = np.array(im).astype(np.float) / 255
fig = plt.figure()
plt.plot(np.arange(10), 4 * np.arange(10))
rect = fig.patch
plt.text(-1, 30, 'Some lovely text n websiteaddress.org ', style='italic',
bbox={'facecolor':'blue', 'alpha':0.5, 'pad':10}, rotation=90)
# can use zorder here rather than hide behind it (e.g. zorder=1)
fig.figimage(im, 0, fig.bbox.ymax - height)
im = Image.open('logo2.png')
height = im.size[1]
im = np.array(im).astype(np.float) / 255
fig.figimage(im, fig.bbox.ymin, 0)
# Saving with the same dpi as the screen default
fig.savefig('temp.png', dpi=80, facecolor=fig.get_facecolor(), edgecolor='none')


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

%d bloggers like this: