Drawing the Butterfly Curve with Python

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.


butter

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

math

math2

It can also be expressed using spherical coordinates:

math3


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)

butter_fly

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")

image_ico

……

阅读全文

python画蝴蝶曲线图

蝴蝶曲线是由Temple H·Fay发现的可用极坐标函数表示的蝴蝶曲线。由于此曲线优美,因此就想把它作为博客favicon.ico,这里我使用python

matplotlib.pyplot包来绘制需要的蝴蝶曲线图。先看下漂亮的蝴蝶曲线吧。

butter

1.首先我们需要确定蝴蝶曲线的函数表达

math

math2

也可用球坐标表示

math3

2.选择python里面的matplotlib.pyplot作为画图工具

1.首先导入python包

import numpy as np
import matplotlib.pyplot as plt

2.设置个参数的值

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.根据公式,使用numpy里面的函数使用plt画出所需图片

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

butter_fly

4.使用Image重新调整图片到适当的大小使其符合favicon大小

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

image_ico

至此,我们完成了使用python的matplotlib.pyplot包绘制漂亮的蝴蝶曲线的过程,把图片保存到网站跟目录就可以看到了!

……

阅读全文

最近文章

分类

标签

友情链接

其它