Matplotlib 原子电子轨道动画:统一实现 8 个方向电子的同步圆周运动

3次阅读

Matplotlib 原子电子轨道动画:统一实现 8 个方向电子的同步圆周运动

本文详解如何使用 matplotlib.animation.FuncAnimation 实现原子壳层中 N、S、E、W 及四个对角(NE、NW、SE、SW)共 8 个电子的一致、平滑、同速圆周运动,纠正常见坐标变换错误,提供可复用的极坐标→直角坐标映射方案。

本文详解如何使用 `matplotlib.animation.funcanimation` 实现原子壳层中 n、s、e、w 及四个对角(ne、nw、se、sw)共 8 个电子的**一致、平滑、同速圆周运动**,纠正常见坐标变换错误,提供可复用的极坐标→直角坐标映射方案。

在构建原子模型动画时,一个典型误区是为不同方位的电子硬编码独立的坐标更新逻辑(如分别处理 x±r·cos、y±r·sin 等组合),这极易导致对角方向电子轨迹偏离圆形——本质原因是未统一采用标准参数化圆周运动公式
[ x(theta) = x_0 + R cdot cos(theta + phi), quad y(theta) = y_0 + R cdot sin(theta + phi) ]
其中 ( (x_0, y_0) ) 是轨道圆心(此处为原点),( R ) 是轨道半径,( phi ) 是初始相位角。八个方向对应相位偏移:

  • E(东):( phi = 0 ) → ( (cos0, sin0) = (1, 0) )
  • N(北):( phi = pi/2 ) → ( (0, 1) )
  • W(西):( phi = pi ) → ( (-1, 0) )
  • S(南):( phi = 3pi/2 ) → ( (0, -1) )
  • NE:( phi = pi/4 ) → ( (sqrt{2}/2, sqrt{2}/2) )
  • NW:( phi = 3pi/4 ) → ( (-sqrt{2}/2, sqrt{2}/2) )
  • SW:( phi = 5pi/4 ) → ( (-sqrt{2}/2, -sqrt{2}/2) )
  • SE:( phi = 7pi/4 ) → ( (sqrt{2}/2, -sqrt{2}/2) )

以下为优化后的完整实现(精简冗余代码,增强可读性与扩展性):

import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation  # 设置画布与轨道参数 fig, ax = plt.subplots(figsize=(6, 6)) ax.set_xlim(-4, 4) ax.set_ylim(-4, 4) ax.set_aspect('equal') ax.axis('off')  R = 3.0  # 轨道半径(简化显示,避免原始代码中过大的 14.5 导致视图溢出) theta_frames = np.linspace(0, 2*np.pi, 120)  # 动画帧数  # 绘制轨道圆 circle = plt.Circle((0, 0), R, fill=False, color='gray', linewidth=1.2) ax.add_patch(circle)  # 定义 8 个电子的初始角度(弧度)和颜色 angles_init = np.array([0, np.pi/2, np.pi, 3*np.pi/2,  # E, N, W, S                         np.pi/4, 3*np.pi/4, 5*np.pi/4, 7*np.pi/4])  # NE, NW, SW, SE colors = ['red', 'blue', 'green', 'orange', 'purple', 'brown', 'pink', 'cyan']  # 创建 8 个电子散点对象 electrons = [] for i, (angle, color) in enumerate(zip(angles_init, colors)):     x0 = R * np.cos(angle)     y0 = R * np.sin(angle)     point, = ax.plot(x0, y0, marker='o', markersize=6, color=color, zorder=10)     electrons.append(point)  # 动画更新函数:所有电子同步绕原点旋转 def update(frame):     # 当前总旋转角度(随帧递增)     total_angle = frame     for i, (point, angle_init) in enumerate(zip(electrons, angles_init)):         # 计算当前时刻位置:圆心(0,0) + 半径R × (cos, sin)含初始相位         x = R * np.cos(total_angle + angle_init)         y = R * np.sin(total_angle + angle_init)         point.set_data(x, y)     return electrons  # 启动动画 anim = FuncAnimation(fig, update, frames=theta_frames,                       interval=50, blit=True, repeat=True) plt.tight_layout() plt.show()

关键改进说明

  • 统一运动模型:摒弃为每个电子写独立 x_ae/y_ae 表达式的做法,改用标准参数方程,确保所有点严格遵循 (x^2 + y^2 = R^2);
  • 相位解耦设计:将初始方位(angles_init)与动态旋转(total_angle)分离,新增电子只需追加角度与颜色,无需修改 update() 内部逻辑;
  • 视觉优化:使用 plt.Circle 绘制清晰轨道线,blit=True 提升动画性能,zorder=10 确保电子始终显示在轨道上方;
  • 可扩展性强:若需添加更多电子(如 d 轨道的 10 个方向),仅需扩展 angles_init 数组即可。

⚠️ 注意事项

  • 原始代码中 shell2 = 14.5 导致 set_xlim(-4,4) 无法显示全部内容,务必使绘图范围 ≥ 轨道半径;
  • 避免在 update() 中重复调用 np.cos/np.sin 计算固定值(如 45*0.0174533),应预计算为 np.pi/4;
  • 若需不同转速,可为各电子引入独立角速度系数 omega[i],修改为 x = R * np.cos(omega[i]*frame + angle_init)。

通过此方案,您将获得一个物理意义准确、代码结构清晰、易于维护的原子电子动态模型——所有电子真正“同轨同速”,完美诠释量子壳层的经典可视化表达。

text=ZqhQzanResources