The Axes method ax.imshow displays an image on the axes. In its basic usage, it takes a two-dimensional array and maps its values to the pixels on an image according to some interpolation scheme and normalization. In this case, im is a three-dimensional array of shape (n, m, 3) in which the “depth” coordinate corresponds to the red, green and blue components of each pixel in the n-by-m image.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
f1 = lambda x, y: (0., 0.16*y)
f2 = lambda x, y: (0.85*x + 0.04*y, -0.04*x + 0.85*y + 1.6)
f3 = lambda x, y: (0.2*x - 0.26*y, 0.23*x + 0.22*y + 1.6)
f4 = lambda x, y: (-0.15*x + 0.28*y, 0.26*x + 0.24*y + 0.44)
fs = [f1, f2, f3, f4]
npts = 50000
# Canvas size (pixels).
width, height = 300, 300
aimg = np.zeros((width, height))
x, y = 0, 0
for i in range(npts):
    # Pick a random transformation and apply it.
    f = np.random.choice(fs, p=[0.01, 0.85, 0.07, 0.07])
    x, y = f(x, y)
    # Map (x, y) to pixel coordinates.
    # NB we "know" that -2.2 < x < 2.7 and 0 <= y < 10.
    ix, iy = int(width / 2 + x * width / 10), int(y * height / 12)
    # Set this point of the array to 1 to mark a point in the fern.
    aimg[iy, ix] = 1
plt.imshow(aimg[::-1,:], cmap=cm.Greens)
plt.savefig('ex422.png', dpi=100)
plt.show()

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading