python - Pyplot 如何绘制数学艺术图

标签 python numpy matplotlib math plot

如何绘制这些循环结构: 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()

但只能得到这个: enter image description here

最佳答案

以报告链接中的第一个示例为例:

enter image description here

因此,您必须使用 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()

enter image description here

关于python - Pyplot 如何绘制数学艺术图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70721139/

相关文章:

python - 按重复列值删除行

python - numpy distutils——尝试编译一些东西并在失败时设置标志

python - 是否可以使用 k-means 在 scikit/sklearn learn 中使用的 K++ 初始化过程?

python - Anaconda ImportError : libSM. so.6: 无法打开共享对象文件: 没有那个文件或目录

python - python中以路径为参数删除特定文件夹

python - 有没有办法使用 numpy 广播来 NOT 任意 M x N 矩阵?

python - Tensorflow:ValueError:应定义 `Dense` 输入的最后一个维度。发现 `None`

python - alembic 如何将所有修订文件合并到一个文件中?

python - Matplotlib:仅一个子图具有相同的纵横比

python - 在 map 上绘制图像