
r=a⋅e^(bθ)其中:
用python绘制黄金螺线的代码如下,采用动态展示方式(可直接运行)
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.animation import FuncAnimation# 黄金比例参数phi = (1 + np.sqrt(5)) / 2b = np.log(phi) / (np.pi / 2)# 生成完整轨迹数据(用于动画分段绘制)theta_full = np.linspace(0, 6 * np.pi, 1500) # 增加圈数和采样点更流畅r_full = np.exp(b * theta_full)x_full = r_full * np.cos(theta_full)y_full = r_full * np.sin(theta_full)# 创建图形fig, ax = plt.subplots(figsize=(7, 7), facecolor='white')ax.set_aspect('equal')ax.axis('off')margin = 1.2 * max(np.abs(x_full).max(), np.abs(y_full).max())ax.set_xlim(-margin, margin)ax.set_ylim(-margin, margin)# 绘制螺旋线line, = ax.plot([], [], 'o-',markersize=2,linewidth=2.5,color='#e67e22', # 橙色系,温暖醒目markevery=[-1]) # 仅在末端显示一个点,模拟"生长"效果# 起点标记(可选)ax.grid(True, linestyle='--', alpha=0.3)ax.axhline(0, color='gray', linewidth=0.5)ax.axvline(0, color='gray', linewidth=0.5)# ax.plot(1, 0, 'o', color='#e74c3c', markersize=6) # 起点红点def init():line.set_data([], [])return line,def animate(i):# 每帧绘制前 i% 的轨迹(平滑过渡)idx = int(i / 100 * len(x_full))line.set_data(x_full[:idx], y_full[:idx])return line,# 创建动画:100帧完成绘制,循环一次ani = FuncAnimation(fig, animate, init_func=init,frames=100, interval=30, blit=True, repeat=True)plt.title('Golden Spiral (Logarithmic Spiral)',fontsize=16, fontweight='bold', color='#2c3e50', pad=15)plt.tight_layout()plt.show()
合集 | 文章 |
|---|---|