As a post-script to last week’s post, here’s a snippet of Python code that uses the wonderful matplotlib to display a given frame of Timepix data. It takes the Comma Separated Value (CSV) file generated by the frame reader and plots a “pixel” at each (x, y) with a colour corresponding to the number of counts measured by that pixel.
I’ve added the Python code to the github repository as display.py, but it’s also included below so you can use it however you like. I’ve been having a lot of fun with IPython and the IPython notebook (which you can find out how to install on your own system here) to do all this on a web browser (screenshot above). But here’s the source code anyway:
###############################################################################
# CERN@school: A very simple Python frame display #
###############################################################################
# Author: T. Whyntie - t.whyntie [at] qmul.ac.uk #
# Date: April 2013 #
###############################################################################
#!/bin/env python
# Import what we need from numpy and matplotlib.
from numpy import loadtxt
from matplotlib import pyplot
from matplotlib.patches import Rectangle
# Open the data file - specify the actual filename between the quotes.
datafile = open("dataout0.csv", "r")
# Get the X, Y and C values from the data file.
X, Y, C = loadtxt(datafile, delimiter=",", unpack=True)
# Set up the plot area for the frame data.
pyplot.figure().add_subplot(111, aspect='equal') # ensure equal aspect ratio.
pyplot.xlim(0,256) # x limits.
pyplot.ylim(0,256) # y limits.
pyplot.gca().patch.set_facecolor((0,0,0)) # Black background.
# Select the "hot" colour map for the pixel counts.
cmap = pyplot.cm.hot
# Loop over the pixels extracted from the data file and draw a "pixel"
# in the plot area.
for x, y, c in zip(X, Y, C):
pyplot.gca().add_patch( \
Rectangle( # We now define the pixel's "Rectangle"; \
(x,y), # The location of the pixel (lower-left corner);
facecolor=cmap(c/C.max()), # The pixel colour (from the map);
edgecolor='none', # Edgeless pixels;
width=1.0, height=1.0, # Specify the size of the pixel.
)\
)
# Display the frame.
pyplot.show()
If you didn’t manage to get the frame reader working/compiling, I’ve put the CSV files of the test dataset on figshare too. Enjoy!







