Art with Math

Last week I bought a plotter. I had it on my wish list for several years, but hadn’t the time for it. The last days I created a lot of stickers – which also is a lot of fun. But it’s not the only fun thing you can do with such a tool. I studied physics and I’m a huge python fan – I love to make plots with python. So I thought, why not using this to create some really fun, beautiful and nerdy postcards 😀

Here are some examples of what worked best:

 

The Lorenz Attractor

That’s a classical one. You can find the equations and code samples on Wikipedia.

Nevertheless, here is the code I used:

import numpy as np
import matplotlib.pyplot as plt

a = 9.81
b = 42
c = np.pi

def new_x(a, y, x, dt):
    new_x = x + (a*(y-x))*dt
    return new_x

def new_y(b, x, y, z, dt):
    new_y = y + (x*(b-z)-y)*dt
    return new_y

def new_z(c, x, y, z, dt):
    new_z = z + (x*y - c*z)*dt
    return new_z
    

def plot_lorenz(ax, number_steps=10000, time_step=0.001, x=0, y=1, z=1.05, a=9.81, b=42, c=np.pi, color='k'): 
    x_werte = np.array([x])
    y_werte = np.array([y])
    z_werte = np.array([z])
    for i in range(number_steps):
        x = new_x(a, y_werte[-1], x_werte[-1], time_step)
        y = new_y(b, x_werte[-1], y_werte[-1], z_werte[-1], time_step)
        z = new_z(c, x_werte[-1], y_werte[-1], z_werte[-1], time_step)
        x_werte = np.append(x_werte, x)
        y_werte = np.append(y_werte, y)
        z_werte = np.append(z_werte, z)     
    ax.plot(x_werte, y_werte, z_werte, lw=0.5, color=color)
    plt.axis('off')
    
    
ax = plt.figure().add_subplot(projection='3d')
plot_lorenz(ax, color='orange')
plot_lorenz(ax, color='silver', c=3)

and here is the corresponding plot, with c=3 for the grey line and c=pi for the orange one.

 

Heart

If the Lorenz Attractor is to chaotic for you, or if you need a nice present for a nerd than you will like the next artwork. It’s more structured but plotted with love. Here is the code:

import numpy as np
import matplotlib.pyplot as plt
  
def plot_heart(ax, widht=1.2, lw=3):
    t=np.linspace(0,1,100)
    x=(-3*t**2 + 2*t+1)*np.abs(np.sin(t))*widht
    y=(-3*t**2 + 2*t+1)*np.abs(np.cos(t))
      
    ax.plot(-x,y, color='lightseagreen', lw=lw)
    ax.plot(x,y, color='lightseagreen', lw=lw)
    plt.axis('off')
    plt.gca().set_aspect('equal')

ws = np.linspace(0, 1.2, 20)    
fig, axes = plt.subplots(1,1, dpi=300)
for w in ws:
    plot_heart(axes, widht=w, lw=1)

 

Sinus Flowers

Last but not least, I have the nice flower plots for you. It’s super simple, but still beautiful. Ingredients of these flowers are only simple sinus functions with altering amplitude and rotated around zero. That’s it. Here’s the code:

import matplotlib.pyplot as plt
import numpy as np

def sinus(x, a=1, b=0, c=1):
    y = a*np.sin(c*x+b) 
    return y

def turn_points(x, y, alpha):
    alpha = np.deg2rad(alpha)
    x_new = x*np.cos(alpha)-y*np.sin(alpha)
    y_new = x*np.sin(alpha)+y*np.cos(alpha)
    return(x_new, y_new)

fig, axes = plt.subplots(1,1, figsize=(10,10))
plt.axis('off')
plt.gca().set_aspect('equal')

x = np.linspace(-np.pi, np.pi, 100)
for a in np.linspace(-1, 1, 5):
    for phi in [0, 45, 90, 135]:
        axes.plot(*turn_points(x, sinus(x, a), phi), color='silver')

 

To make the postcards, I used a common printer to print the code in light grey on the card. Than I used my plotter to draw the function on the card. If you don’t have a plotter, you can print both, the code and the function, with your printer on the postcard – it will look as well beautiful 🙂

Happy plotting!