Monthly Archives: March 2012

Code release: LTB in Python, spherical collapse, and Buchert averaging

The release of our next paper is imminent (yay!), and so it’s time for another code release. I try to make all of my code, or at least a substantial fraction of it, publicly available. This enables other people to reproduce and check my work if they want to. It also allows them to build off my code and do cool new things, rather than having to spend months solving problems that, well, have already been solved. That’s the theory, anyway – I only know of a couple of people who’ve actually poked around in the code, or tried to use it for something. But hey, you’ve got to start somewhere. For posterity, I’ve posted the closest thing I have to release notes below.

Continue reading

Custom dash/dot line styles in matplotlib

I’ve recently been polishing a number of graphs to get them up to publication quality (coming soon!), and one of the necessary customisations that came up was the style of dashed/dotted lines. Matplotlib’s default dotted and dash/dotted line styles aren’t very clear in a lot of plots, and can be difficult to distinguish (e.g. the dash/dotted style looks too much like dots).

Happily, you can make your own custom line styles, using a sequence (list) of numbers and the set_dashes() method of the Line2D object. To do this:

import pylab as P
line = P.plot(x, y) # 'line' is actually a list of lines. We want line[0]
seq = [2, 4, 7, 4]

Here, seq is the alternating sequence of dashes and spaces. seq[0] draws a line of length 2, seq[1] draws a space (i.e. no line) of length 4, seq[2] a line of length 7, and seq[3] a space of length 4.

You can carry on this sequence indefinitely if you like; even indices are drawn as lines, odd indices as spaces. For example, seq = [2, 2] would look like dots, and seq = [7, 4, 3, 4] is a clearer dash-dotted line than the default. You can even use float lengths for the dashes, rather than integers.

(There’s a rather spartan example on the matplotlib website too.)