【问题标题】:Pyplot how to plot math artPyplot如何绘制数学艺术
【发布时间】:2022-01-15 11:50:10
【问题描述】:

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

但只能得到这个:

【问题讨论】:

    标签: python numpy matplotlib math plot


    【解决方案1】:

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

    所以你必须用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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2012-12-29
      相关资源
      最近更新 更多