- Применить fade_in on x ;
- Применить fade_in y ; fade_out on y ;
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib.animation import FuncAnimation, FFMpegWriter
def chain_animations(animations):
# Extract all update functions and frame counts
update_funcs = [a._func for a in animations]
frame_counts = [a.frame_seq.__length_hint__() for a in animations]
intervals = [a._interval for a in animations]
figs = [a._fig for a in animations]
assert all(interval == intervals[0] for interval in intervals), "All animations must have the same interval"
assert all(fig is figs[0] for fig in figs), "All animations must use the same figure"
total_frames = sum(frame_counts)
cumulative = [0]
for count in frame_counts:
cumulative.append(cumulative[-1] + count)
def update(frame):
for i in range(len(frame_counts)):
if cumulative 1 else 1.0
current_alpha = t * target_alpha
self.alpha = current_alpha
self.patch.set_alpha(current_alpha)
return [self.patch]
return FuncAnimation(ax.figure, animate, frames=total_frames, interval=1000/fps, blit=True)
def fade_out(self, ax, time=1.0, fps=30):
total_frames = max(1, int(time * fps)) # avoid divide by zero
target_alpha = 1.0
self.alpha = 1.0
if self.patch is not None:
self.patch.set_alpha(1.0)
def animate(frame):
t = frame / (total_frames - 1) if total_frames > 1 else 1.0
current_alpha = (1 - t) * target_alpha
self.alpha = current_alpha
self.patch.set_alpha(current_alpha)
return [self.patch]
return FuncAnimation(ax.figure, animate, frames=total_frames, interval=1000/fps, blit=True)
import matplotlib.pyplot as plt
from Symbol import Symbol
fig, ax = plt.subplots()
ax.set_xlim(0, 200)
ax.set_ylim(0, 200)
# Create a Symbol instance
x = Symbol(ax, letter='$x$', size=30, color='blue', position=(50, 50))
y = Symbol(ax, letter='$y$', size=30, color='red', position=(55, 50))
# Doesn't work: x fade in, y fade in, y fade out
# Works: x fade in, y fade in, x fade out
anim1 = x.fade_in(ax, time=1.0, fps=30)
anim2 = y.fade_in(ax, time=1.0, fps=30)
anim3 = y.fade_out(ax, time=1.0, fps=30)
anim = chain_animations([anim1, anim2, anim3])
anim.save("fade.mp4", writer=FFMpegWriter(fps=30))
Подробнее здесь: https://stackoverflow.com/questions/796 ... mations-at