Home

Graphics

Graphics in Python is done not by the core language, but by separate libraries that extend the language with graphical functions. There are several such libraries, of which Hello World covers pygame in some detail. We will use matplotlib, which is designed for scientific work. Rather than importing matplotlib directly, however, we will mostly import pylab, which is an extension of matplotlib to follow the conventions of MATLAB.

The following example summarizes what is really essential for scientific figures.

from numpy import linspace, pi, cos, sin
from pylab import figure, show

phi = (1+5**.5)/2
n = linspace(1,100,99)
x = n**.5 * cos(2*pi*phi*n)
y = n**.5 * sin(2*pi*phi*n)
fig = figure()
panel = fig.add_subplot(1,1,1,axisbg='black')
panel.scatter(x,y,s=80,color='yellow') #draws data points
panel.set_xlabel('horizontal')
panel.set_ylabel('vertical')
panel.set_aspect('equal')
show()

It draws a sunflower pattern.

Note the use of an array to evaluate a function at many points with one command. To join the points, you can use

plot(x,y,color='magenta')

instead of scatter().

They keyword arguments axisbg and s and color are all optional, and have sensible defaults.

If you want to hide the numbers on the axis, you can use

panel.xaxis.set_visible(False)
panel.yaxis.set_visible(False)

When you have an array containing errors in the y-direction called errors, you can draw a curve with error bars as follows (this replaces the use of plot()):

panel.errorbar(x, y, yerr=errors)

A title and legend can be added using:

panel.set_title('Title')
panel.legend(('label1','label2'))

For a really minimal figure, you can reduce the plotting part of the above program, to the following.

scatter(x,y)
plot(x,y)
show()

The shell of Nautilus is a spiral of the type

x=γ 2t cos(2πt)
y=γ 2t sin(2πt)

growing by a factor of γ every quarter turn. It is often thought stated, that a Nautilus shell has

γ = (1+√5)/2

making it a 'golden spiral', but in fact the growth factor γ is a little smaller than this.

Draw such a spiral, for a suitable value of γ

DNA damage repair

LEM-3 is a nuclease involved in the DNA damage response in C. elegans. Below you see its effects on the sensitivity to X-rays (estimated from Dittrich et al., 2012).

X-ray dose (Gy) Embryonic lethality in wild-type (% +/- s.d.) Embryonic lethality in lem-3 mutants (% +/- s.d.)
0 0 +/- 0 0 +/- 0
5 0 +/- 0 42 +/- 2
15 3 +/- 2 72 +/- 6
30 10 +/- 4 89 +/- 4
60 50 +/- 9 100 +/- 0

Plot the data above. Give the curves different colors and include axis labels, title and legend.

Submit the program

Pentagrams

This topic is optional

The following code draws a hexagon:

from numpy import linspace, pi, cos, sin, exp
from pylab import plot
N = 6
t = linspace(0,2*pi,N+1)
x = cos(t)
y = sin(t)
plot(x,y)

The same can also be done using complex numbers. In that case x and y are defined as follows:

z = exp(t*1j)
x,y = z.real,z.imag

Modify the hexagon example above to produce a pentagram, using complex numbers.

Submit the program

Animations

from matplotlib.animation import FuncAnimation
from pylab import figure, show
from numpy import pi, linspace, cos, sin

fig = figure()
panel = fig.add_subplot(1,1,1)

N = 6
t = linspace(0,2*pi,N+1)
dt = 0.02
R = 1.2

def frame(n):
    panel.clear()
    x = cos(t+n*dt)
    y = sin(t+n*dt)
    panel.set_xlim(-R,R)
    panel.set_ylim(-R,R)
    panel.set_aspect('equal')
    panel.plot(x,y)
    
dummy = FuncAnimation(fig,frame,range(1000),interval=25)
show()