A blog of Python-related topics and code.

Processing UK Ordnance Survey terrain data

The UK's Ordnance Survey mapping agency now makes its 50 m resolution elevation data freely-available through its online OpenData download service. This article uses Python, NumPy and Matplotlib to process and visualize these data without using a specialized GIS library.

Visualizing uncertainties in plotted data

The equation for the temperature-dependence of the diffusion of hydrogen in tungsten may be written in Arrhenius form: $$ k = A\exp\left(-\frac{E}{T}\right) \quad \Rightarrow \; \ln k = \ln A - \frac{E}{T}, $$ where the temperature, $T$, and activation energy, $E$, are expressed in eV and the pre-exponential Arrhenius parameter, $A$, and rate constant, $k$, take units of $\mathrm{m^2\,s^{-1}}$. From the study of Frauenfelder [1] the parameters $A$ and $E$ may be associated with uncertainties as follows: $$ \begin{align*} A & = (4.1 \pm 0.5) \times 10^{-7}\;\mathrm{m^2\,s^{-1}}, \\ E &= 0.39 \pm 0.08 \;\mathrm{eV}. \end{align*} $$ These uncertainties can be propagated to the expression for $\ln k$: $$ \sigma_{\ln k} \approx \sqrt{ \left( \frac{\sigma_A}{A} \right)^2 + \left( \frac{\sigma_E}{T} \right)^2 }. $$ If we assume the uncertainty remains normally-distributed, Matplotlib's imshow function can be used to illustrate the Arrhenius equation for this data.

Turning an image into a sticker chart

To produce a children's sticker chart from a provided image, the following code divides it into squares (which can be cut out) and produces further images with matching labels for the reverse side of the printed image and a piece of card onto which the squares can be stuck (you need to provide your own glue).

The distribution of Easter dates

Loosely speaking, in the Gregorian calendar, Easter falls on the first Sunday following the first full Moon on or after 21 March. There are various algorithms (Computus) which can be used to calculate its date, and the cycle of dates repeats every 5.7 million years. The following code produces a bar chart of the distribution of Easter dates using one of these algorithms.

Linear transformations on the two-dimensional plane

A linear transformation in two dimensions can be visualized through its effect on the two orthonormal basis vectors $\hat{\imath}$ and $\hat{\jmath}$. In general, it can be represented by a $2 \times 2$ matrix, $\boldsymbol{T}$, which acts on a vector $v$ to map it from a vector space spanned by one basis onto a different vector space spanned by another basis: $\boldsymbol{v'} = \boldsymbol{T}\boldsymbol{v}$. This change of basis can be visualized by drawing the basis vectors in the two-dimensional plane, along with equally-spaced "grid lines" parallel to each of them. A linear transformation keeps the grid lines evenly spaced, and the origin fixed.