Lesson 1: Arrays and angles
Build the golden angle in Python, and learn on the way:
- numpy arrays with np.arange
- arithmetic on a whole array at once
- radians, np.cos and np.sin
- from an angle and a distance to x and y
- a scatter plot coloured by a value
Watch it first
Plays from YouTube after a click.
The script
Open in Colab: this lesson as a notebook"""Golden angle: seed n at the angle n x 137.5 degrees and the distance sqrt(n) (Vogel, 1979).
Run: python golden-angle.py (needs: pip install numpy matplotlib)
"""
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("dark_background")
n = np.arange(1, 1001) # seed numbers
golden = np.pi * (3 - np.sqrt(5)) # 137.5 degrees in radians: 360 / phi**2
theta, r = n * golden, np.sqrt(n) # the whole rule
x, y = r * np.cos(theta), r * np.sin(theta)
plt.figure(figsize=(6, 6))
plt.scatter(x, y, s=8, c=n, cmap="viridis")
plt.axis("equal")
plt.axis("off")
plt.show()
# Try: golden = np.radians(144) -> five straight spokes (144 degrees is 2/5 of a turn)
No Python on this computer? The notebook runs the same script, the exercises and the answers in your browser, on Google Colab.
Line by line
n = np.arange(1, 1001)An array of the numbers 1 to 1000, one per seed. np.arange stops just before its end, so 1001 gives 1000.
golden = np.pi * (3 - np.sqrt(5))The golden angle in radians, the unit numpy’s trigonometry uses: a full turn is 2π. π·(3 − √5) ≈ 2.39996 radians, which is 137.5°.
theta, r = n * golden, np.sqrt(n)Arithmetic on an array works on every element: n * golden is 1000 angles at once, np.sqrt(n) 1000 distances. No loop is needed.
x, y = r * np.cos(theta), r * np.sin(theta)From an angle and a distance (polar coordinates) to a point on the page: x = r·cos θ, y = r·sin θ.
plt.scatter(x, y, s=8, c=n, cmap="viridis")One dot per seed, size 8, each coloured from the “viridis” colour map by its number n.
plt.axis("equal")The same scale on both axes, so the flower comes out round, not squashed.
Try it yourself
Print the golden angle in degrees.
Hint
np.degrees turns radians into degrees.
Answer
print(np.degrees(golden)) prints 137.50776405003785.
Draw only the first 100 seeds.
Hint
Change the end of np.arange.
Answer
n = np.arange(1, 101). With only 100 seeds the families of spirals are much harder to see: the pattern needs many seeds.
Make the outer seeds bigger than the inner ones.
Hint
s, the size, can be an array too.
Answer
plt.scatter(x, y, s=n / 50, c=n, cmap="viridis"): seed 1000 gets size 20, seed 50 size 1.
Quiz
Flashcards
Read the front, say the back to yourself, then open the card.
np.arange(a, b)
The numbers from a up to, but not including, b, as a numpy array.
Vectorised code
Arithmetic on whole arrays at once instead of a loop over items: shorter and faster.
Radians
The angle unit of np.cos and np.sin: 2π for a full turn. np.radians(144) converts from degrees.
Polar to x, y
x = r·cos θ, y = r·sin θ.
plt.scatter(x, y, c=values)
One dot per point; c colours each one by a value through the colour map cmap.