使用matplotlib和函数动画无法旋转原子模型中的所有电子

问题描述 投票:0回答:1

我正在尝试创建一个 matplotlib 动画,其中的点位于 N、W、E、S、NW、NE、SW 和 SE 处,以循环运行(就像原子壳中的 8 个电子)。当 N、W、E 和 S 工作时,NW、NE、SW 和 SE 不符合同一环路。有人可以通过一些解释来帮助完成所需的转换吗?

import matplotlib.pyplot as plt  
import numpy as np
from matplotlib.animation import FuncAnimation, PillowWriter

fig,ax = plt.subplots()
ax.clear()
ax.set_xlim(-4,4)
ax.set_ylim(-4,4)

shell2 = 14.5

largest = int(14.5*2)

difference = int(0-(largest))
difference2 = int(0+(largest))


def createList(r1, r2):
    return [item for item in range(r1, r2+1)]


x = (createList(difference,difference2))
y = (createList(difference,difference2))


def rotate_anim():
  theta = np.linspace(0, 2*np.pi, 100)

  s2 = np.sqrt(shell2**2)
  s2x1 = s2*np.cos(theta)
  s2x2 = s2*np.sin(theta)
  ax.plot(s2x1,s2x2, color='k')
    
  theta2 = np.linspace(0, 2*np.pi, 100)


  x_e1s2 = shell2
  y_e1s2 = 0
  e1s2_circlemove ,= ax.plot(x_e1s2, y_e1s2, marker='.', color='r') 

  x_e2s2 = -shell2
  y_e2s2 = 0
  e2s2_circlemove ,= ax.plot(x_e2s2, y_e2s2, marker='.', color='k')

  x_e3s2 = 0
  y_e3s2 = shell2
  e3s2_circlemove ,= ax.plot(x_e3s2, y_e3s2, marker='.',  color='k')

  x_e4s2 = 0
  y_e4s2 = -shell2
  e4s2_circlemove ,= ax.plot(x_e4s2, y_e4s2, marker='.',  color='r')


  x_e5s2 = (shell2)*np.cos(45*0.0174533)
  y_e5s2 = (shell2)*np.sin(45*0.0174533)
  e5s2_circlemove ,= ax.plot(x_e5s2, y_e5s2, marker='.', markersize=20,  color='r')



  ax.set_aspect(1)


  plt.xlim(difference,difference2)
  plt.ylim(difference,difference2)




  def update(angle):

        r_ae = shell2
        x_ae = x_e1s2 + r_ae * np.cos(angle) - shell2 
        y_ae = y_e1s2 + r_ae * np.sin(angle)
        e1s2_circlemove.set_data(x_ae, y_ae)

        r_ae = shell2
        x_ae = x_e2s2 - r_ae * np.cos(angle) + shell2 
        y_ae = y_e2s2 - r_ae * np.sin(angle)
        e2s2_circlemove.set_data(x_ae, y_ae)

        r_ae =  shell2
        x_ae = x_e3s2 - r_ae * np.sin(angle) 
        y_ae = y_e3s2 + r_ae * np.cos(angle) - shell2 
        e3s2_circlemove.set_data(x_ae, y_ae)

        r_ae =  shell2
        x_ae = x_e4s2 + r_ae * np.sin(angle) 
        y_ae = y_e4s2 - r_ae * np.cos(angle) + shell2 
        e4s2_circlemove.set_data(x_ae, y_ae)


        # NOT WORKING - not running in the circular loop
        r_ae =  shell2
        x_ae = (shell2)*np.cos(45*0.0174533) + r_ae * np.sin(angle)  
        y_ae = (shell2)*np.sin(45*0.0174533) -  r_ae * np.cos(angle)  + shell2  
        e5s2_circlemove.set_data(x_ae, y_ae)

        return e1s2_circlemove, e2s2_circlemove, e3s2_circlemove, e4s2_circlemove, e5s2_circlemove,

  plt.grid(False)
  plt.axis('off')
  anim = FuncAnimation(fig, update, frames=theta2, repeat=True)

  plt.show()

rotate_anim()
python matplotlib animation
1个回答
0
投票

我想通了。下面是代码。

'''

import matplotlib.pyplot as plt  
import numpy as np
from matplotlib.animation import FuncAnimation, PillowWriter

fig,ax = plt.subplots()
ax.clear()
ax.set_xlim(-4,4)
ax.set_ylim(-4,4)

shell2 = 14.5

largest = int(14.5*2)

difference = int(0-(largest))
difference2 = int(0+(largest))


def createList(r1, r2):
    return [item for item in range(r1, r2+1)]


x = (createList(difference,difference2))
y = (createList(difference,difference2))


def rotate_anim():
  theta = np.linspace(0, 2*np.pi, 100)

  s2 = np.sqrt(shell2**2)
  s2x1 = s2*np.cos(theta)
  s2x2 = s2*np.sin(theta)
  ax.plot(s2x1,s2x2, color='k')
    
  theta2 = np.linspace(0, 2*np.pi, 100)

  x_e1s2 = shell2
  y_e1s2 = 0
  e1s2_circlemove ,= ax.plot(x_e1s2, y_e1s2, marker='.', color='r') 




  x_e3s2 = 0
  y_e3s2 = shell2
  e3s2_circlemove ,= ax.plot(x_e3s2, y_e3s2, marker='.',  color='k')



  x_e5s2 = (shell2)*np.cos(45*0.0174533)
  y_e5s2 = (shell2)*np.sin(45*0.0174533)
  e5s2_circlemove ,= ax.plot(x_e5s2, y_e5s2, marker='.',   color='r')



  ax.set_aspect(1)


  plt.xlim(difference,difference2)
  plt.ylim(difference,difference2)




  def update(angle):


        x_ae = shell2 + shell2 * np.cos(angle) - shell2 
        y_ae = 0 + shell2 * np.sin(angle)
        e1s2_circlemove.set_data(x_ae, y_ae)


        x_ae = 0- shell2 * np.sin(angle) 
        y_ae = shell2 + shell2 * np.cos(angle) - shell2 
        e3s2_circlemove.set_data(x_ae, y_ae)

        x_ae = (shell2 + shell2 * np.cos(angle) - shell2 + 0- shell2 * np.sin(angle) )*0.70710678118
        y_ae = (0 + shell2 * np.sin(angle)+shell2 + shell2 * np.cos(angle) - shell2)*0.70710678118
        e5s2_circlemove.set_data(x_ae, y_ae)

        return  e1s2_circlemove, e3s2_circlemove,  e5s2_circlemove,

  plt.grid(False)
  plt.axis('off')
  anim = FuncAnimation(fig, update, frames=theta2, repeat=True)

  plt.show()

rotate_anim();

'''

© www.soinside.com 2019 - 2024. All rights reserved.