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

2024-10-04 09:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的代码版本,上面有一颗围绕一个点运行的“卫星”。我把“卫星”作为一个类,这样我就可以有多个“卫星”同时围绕着这个点运行。然而,我似乎不能使它动画所有的两个例子卫星。我想让所有的动画都显示在同一个图形上。 我设置动画的方式是围绕中心点创建一个圆形路径,让动画只显示卫星沿着该点移动。 我在代码中加入了分解和直接复制部分,这样人们可以更容易地理解我的逻辑。谢谢你的帮助!!!!你知道吗

我知道我可以在同一个图形上同时绘制多个类对象,但不确定动画是否相同。我使用类在更详细的代码中绘制了多个中心点。你知道吗

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)

Tags: theimportselfinitdefasnpfig
1条回答
网友
1楼 · 发布于 2024-10-04 09:18:29

每个图形需要一个动画。所以我可以创建任意多个卫星,并使用一个类来设置它们的动画。可能是这样的:

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()

相关问题 更多 >