Beginner · Golden angle

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.

golden-angle.py
"""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

  1. 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.

  2. 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°.

  3. 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.

  4. 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 θ.

  5. 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.

  6. plt.axis("equal")

    The same scale on both axes, so the flower comes out round, not squashed.

Try it yourself

  1. Print the golden angle in degrees.

    Hint

    np.degrees turns radians into degrees.

    Answer

    print(np.degrees(golden)) prints 137.50776405003785.

  2. 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.

  3. 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

1. What does np.arange(1, 1001) contain?
2. Why is there no for loop in the script?
3. In which unit do np.cos and np.sin take angles?
4. Which turns an angle θ and a distance r into a point?
5. What do you get with golden = np.radians(144)?

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.

These flashcards on their own page →