Исправляет анимацию в matplotlib.Python

Программы на Python
Anonymous
Исправляет анимацию в matplotlib.

Сообщение Anonymous »

Я пытаюсь создать простую анимацию частиц, используя matplotlib, вот так:

Код: Выделить всё

import numpy as np
from matplotlib import pyplot as plt
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))
patch = plt.Circle((5, -5), 0.75, fc='y')

def init():
patch.center = (5, 5)
ax.add_patch(patch)
return patch,

def animate(i):
x, y = patch.center
x = 5 + 3 * np.sin(np.radians(i))
y = 5 + 3 * np.cos(np.radians(i))
patch.center = (x, y)
return patch,

anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=36,
interval=20,
blit=True)

HTML(anim.to_jshtml())
Теперь я хочу обернуть объект plt.Circle в класс «Particle», чтобы можно было добавить другие функции (например, придать ему скорость и т. д.). Вот что я пробовал:

Код: Выделить всё

import numpy as np
from matplotlib import pyplot as plt
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))

class Particle:
def __init__(self,x,y):
self.x = x
self.y = y
def show(self):
return plt.Circle((self.x, self.y), 0.75, fc='y')

p = Particle(5, -5)

def init():
ax.add_patch(p.show())
return p.show(),

def animate(i):
p.x = 5 + 3 * np.sin(np.radians(i))
p.y = 5 + 3 * np.cos(np.radians(i))
return p.show(),

anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=36,
interval=20,
blit=True)

HTML(anim.to_jshtml())
почему это не работает?
Я ожидал, что это будет вести себя так же, как и первый код, поскольку p.show() возвращает plt.Объект Circle.

Подробнее здесь: https://stackoverflow.com/questions/787 ... matplotlib

Вернуться в «Python»