具有相同 class 变量的 Matplotlib 多个动画

Matplotlib multiple animations with same class variables

我有一个简单版本的代码,其中有一个 "satellite" 围绕一个点。我将 "satellite" 设为 class,这样我就可以同时让多个 "satellites" 围绕该点运行。但是,我似乎无法让它为所有两个示例卫星制作动画。我想让所有动画显示在同一个图形上。 我对其进行动画处理的方式是围绕中心点创建一条圆形路径,并让动画只显示卫​​星沿该点移动。 我放入了代码的分解和直接复制部分,以便人们可以更容易地理解我的逻辑。感谢您的帮助!!!!

我知道我可以在同一个图形上同时绘制多个 class 对象,但不确定动画是否相同。我使用 classes.

在我的多个中心点的更详细代码上进行了绘图
import math
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as spi
from operator import itemgetter
from matplotlib import animation

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)

ax = plt.axes(xlim=(0, 10), ylim=(0, 10))

a = plt.Circle((5,5), radius = 0.2, color = 'r') 
plt.gca().add_patch(a)
plt.show()


class Satellite:   #defines the class satellites and the ratio radius 
    def __init__(self, x, y, radius) :
        self.x= x
        self.y= y
        self.radius = radius
        self.forceScale()


    def forceScale(self) :
        z= float(self.radius)
        orbitalRadius = ((1/10) * (z)) + 1
        print(orbitalRadius)

        if orbitalRadius >= 0 :
            Satellite = plt.Circle((5, 5), 0.1, fc='y')        
            def init():
                Satellite.center = (5, 5)
                plt.gca().add_patch(Satellite)
                return Satellite,
            def animate1(n):
                x, y = Satellite.center
                x = self.x + orbitalRadius * np.sin(np.radians(n))
                y = self.y + orbitalRadius * np.cos(np.radians(n))
                Satellite.center = (x, y)
                return Satellite,


        else :
            print("Please check data indices")  

        global anim

        anim = animation.FuncAnimation(fig, animate1, 
                                   init_func= init, 
                                   frames=360, 
                                   interval= 20,
                                   blit=True)
        plt.show()

firstPoint = Satellite(x= 5, y=5, radius = 2) 
secondPoint = Satellite(x=5, y= 5, radius = 4)

###################################################################    
#below is a break down of each section of the code for easier understanding
import math
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as spi
from operator import itemgetter
from matplotlib import animation


#creating the figure grid info
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)


#the central point
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))

a = plt.Circle((5,5), radius = 0.2, color = 'r') #plotting the central point
plt.gca().add_patch(a)
plt.show()


class Satellite:   
#defines the satellite and the radius index(will be used to find actual radius
    def __init__(self, x, y, radius) :
        self.x= x
        self.y= y
        self.radius = radius
        self.forceScale()

#the code which creates the orbit
    def forceScale(self) :
        z= float(self.radius) #making the radius index a float to calculate
        orbitalRadius = ((1/10) * (z)) + 1
        print(orbitalRadius) #checking the calculation works

        if orbitalRadius >= 0 : 
#this part is similar to my more detailed code so it answers can be more easily applied

            Satellite = plt.Circle((5, 5), 0.1, fc='y')
#a base for the how the satellite will look        
            def init():
                Satellite.center = (5, 5) #actual central point of orbit
                plt.gca().add_patch(Satellite)
                return Satellite,
            def animate1(n):
                x, y = Satellite.center
                x = self.x + orbitalRadius * np.sin(np.radians(n))
                y = self.y + orbitalRadius * np.cos(np.radians(n))
                Satellite.center = (x, y)
                return Satellite,
            #creating the path for the satellite

        else :
            print("Please check data indices")  #a checking section


#the actual animation function
        global anim


        anim = animation.FuncAnimation(fig, animate1, 
                                   init_func= init, 
                                   frames=360, 
                                   interval= 20,
                                   blit=True)
        plt.show()






# now making the orbit path for the satellite  
# Satellite information
#the examples
firstPoint = Satellite(x= 5, y=5, radius = 2) 
secondPoint = Satellite(x=5, y= 5, radius = 4)

每个人物需要一个动画。所以我会根据需要创建尽可能多的卫星,并使用单个 class 为它们制作动画。这可能看起来像这样:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

class Satellite:   #defines the class satellites and the ratio radius 
    def __init__(self, x, y, radius, ax=None) :
        self.ax = ax or plt.gca()
        self.x = x
        self.y = y
        self.radius = radius
        self.forceScale()

    def forceScale(self) :
        z= float(self.radius)
        self.orbitalRadius = ((1/10) * (z)) + 1
        print(self.orbitalRadius)
        self.satellite = plt.Circle((5, 5), 0.1, fc='y')
        self.ax.add_patch(self.satellite)

    def ani_init(self):
        self.satellite.center = (5, 5)
        return [self.satellite]

    def ani_update(self, n):
        x, y = self.satellite.center
        x = self.x + self.orbitalRadius * np.sin(np.radians(n))
        y = self.y + self.orbitalRadius * np.cos(np.radians(n))
        self.satellite.center = (x, y)
        return [self.satellite]



class AnimatedUniverse():
    def __init__(self, fig, satellites):
        self.fig = fig
        self.satellites = satellites

    def ani_init(self):
        artists = []
        for sat in self.satellites:
            artists.extend(sat.ani_init())
        return artists

    def animate(self, n):
        artists = []
        for sat in self.satellites:
            artists.extend(sat.ani_update(n))
        return artists

    def start_ani(self):
        self.anim = animation.FuncAnimation(fig, self.animate, 
                                   init_func= self.ani_init, 
                                   frames=360, 
                                   interval= 20,
                                   blit=True)


fig, ax = plt.subplots()
ax.set(xlim=(0, 10), ylim=(0, 10))
ax.set_aspect("equal", adjustable="box")

a = plt.Circle((5,5), radius = 0.2, color = 'r') 
ax.add_patch(a)

firstPoint = Satellite(x= 5, y=5, radius = 2) 
secondPoint = Satellite(x=5, y= 5, radius = 4)
au = AnimatedUniverse(fig, (firstPoint, secondPoint))
au.start_ani()
plt.show()