Pyplot如何绘制数学艺术

Pyplot how to plot math art

如何绘制这些圆形结构: https://blogs.scientificamerican.com/guest-blog/making-mathematical-art/

在 pyplot 中?我试过这个:

x = np.arange(1,11) 
def f(x):
    return np.cos((10*np.pi*x)/14000)*(1-(1/2)*(np.square(np.cos((16*np.pi*x)/16000))))
def z(x):
    return np.sin((10*np.pi*x)/14000)*(1-(1/2)*(np.square(np.cos((16*np.pi*x)/16000))))
def w(x):
    return 1/200 + 1/10*np.power((np.sin(52*np.pi*x)/14000),4)

plt.ylim(-10, 10)
plt.title("Matplotlib demo") 
plt.xlabel("x axis caption") 
plt.ylabel("y axis caption") 
x1=np.linspace(0,14000)

results=f(x1)
results2 = z(x1)
results3 = w(x1)

plt.plot(results)
plt.plot(results2)
plt.plot(results3)


plt.show()

但只得到这个:

以报告中的第一个例子为例 link:

所以你必须用 k 从 1 到 N = 14000 做一个 for 循环,在每次迭代中你画一个半径为 R 并以 (X, Y) 为中心的圆上述等式中的定义:

N = 14000

for k in range(1, N + 1):
    X = cos(10*pi*k/N)*(1 - 1/2*(cos(16*pi*k/N))**2)
    Y = sin(10*pi*k/N)*(1 - 1/2*(cos(16*pi*k/N))**2)
    R = 1/200 + 1/10*(sin(52*pi*k/N))**4

此时你有圆心和半径的坐标,但还没有圆本身,所以你必须计算它。首先,你必须定义一个从 02*pi 的角度 theta,然后计算圆的点:

N = 14000
theta = np.linspace(0, 2*pi, 361)

for k in range(1, N + 1):
    X = cos(10*pi*k/N)*(1 - 1/2*(cos(16*pi*k/N))**2)
    Y = sin(10*pi*k/N)*(1 - 1/2*(cos(16*pi*k/N))**2)
    R = 1/200 + 1/10*(sin(52*pi*k/N))**4

    x = R*np.cos(theta) + X
    y = R*np.sin(theta) + Y

最后,您可以在每次迭代中绘制圆圈。

完整代码

import numpy as np
import matplotlib.pyplot as plt
from math import sin, cos, pi


N = 14000
theta = np.linspace(0, 2*pi, 361)

fig, ax = plt.subplots(figsize = (10, 10))

for k in range(1, N + 1):
    X = cos(10*pi*k/N)*(1 - 1/2*(cos(16*pi*k/N))**2)
    Y = sin(10*pi*k/N)*(1 - 1/2*(cos(16*pi*k/N))**2)
    R = 1/200 + 1/10*(sin(52*pi*k/N))**4

    x = R*np.cos(theta) + X
    y = R*np.sin(theta) + Y

    ax.plot(x, y, color = 'blue', linewidth = 0.1)

plt.show()