BOBOBK

Drawing the Butterfly Curve with Python

TECHNOLOGY

The butterfly curve, discovered by Temple H. Fay, is a beautiful curve that can be expressed using a polar coordinate function. Because of its elegance, I wanted to use it as my blog’s favicon.ico. Here, I’ll use Python’s matplotlib.pyplot package to draw the desired butterfly curve. First, let’s admire the beautiful butterfly curve.


1. First, We Need to Define the Mathematical Expression of the Butterfly Curve

It can also be expressed using spherical coordinates:


2. Choosing matplotlib.pyplot as the Plotting Tool in Python

1. First, import the necessary Python packages

import numpy as np
import matplotlib.pyplot as plt

2. Set the parameter values

t = np.arange(0.0, 12*np.pi, 0.01)
x = np.sin(t)*(np.e**np.cos(t) - 2*np.cos(4*t)-np.sin(t/12)**5)
y = np.cos(t)*(np.e**np.cos(t) - 2*np.cos(4*t)-np.sin(t/12)**5)

3. According to the formula, use numpy functions with plt to draw the required image

plt.figure(figsize=(8,6))
plt.axis('off')
plt.plot(x,y,color='blue',linewidth = '2')
#plt.show()
plt.savefig("butter.jpg",dpi=400)

4. Use Pillow to resize the image to an appropriate size for a favicon

from PIL import Image
im = Image.open("butter.jpg")
favicon = im.resize((50,50))
favicon.save("favicon.ico")


And with that, we have completed the process of drawing the beautiful butterfly curve using Python’s matplotlib.pyplot package! Save the image to your website’s root directory and you’ll see it.

Related