
Вышеуказанное является результатом выполнения приведенного ниже. Вы увидите, что это не совсем работает, потому что область вокруг области графика и области легенды упорно остается белой. Я пробовал множество вариантов приведенного ниже примера, но так и не смог понять.
Я также пытаюсь избежать зацикливания. Повтор = False, похоже, этого не делает. Но это второстепенный вопрос. Главный из них: как добиться, чтобы фон фигуры менял цвет во время анимации?
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import matplotlib as mpl
# Data for the plot
x1 = np.linspace(1,100)
y1 = np.sin(x1)
#####################################
# STEP 1: FADE TO WHITE - FAILED
#####################################
# Create the figure and axis
fig, ax = plt.subplots()
# Plot the data
line1, = ax.plot(x1, y1, label='sin')
# Set the title and axis labels
ax.set_title('Title')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
# Add a legend
#ax.legend(loc='right')
legend = ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.subplots_adjust(right=0.79)
# Function to update the plot for the animation
def update1(frame):
if frame>=noFrames:
frame=noFrames-1
startWhite = tuple([(noFrames-frame-1)/(noFrames-1)]*3)
startBlack = tuple([frame/(noFrames-1)]*3)
ax.cla()
# Plot the data
line1, = ax.plot(x1, y1, label='sin')
# Set the title and axis labels
ax.set_title('Title',color=startWhite)
ax.set_xlabel('X Axis',color=startWhite)
ax.set_ylabel('Y Axis',color=startWhite)
# Add a legend
#ax.legend(loc='right')
legend = ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.subplots_adjust(right=0.79)
fig.patch.set_color(None)
fig.patch.set_facecolor(startBlack)
ax.patch.set_facecolor(startBlack)
fig.patch.set_edgecolor(startBlack)
fig.patch.set_color(startBlack)
plt.rcParams['axes.facecolor'] = startBlack
plt.rcParams['axes.edgecolor'] = startBlack
plt.rcParams['axes.labelcolor'] = startWhite
plt.rcParams['axes.titlecolor'] = startWhite
plt.rcParams['legend.facecolor'] = startBlack
plt.rcParams['legend.edgecolor'] = startWhite
plt.rcParams['legend.labelcolor'] = startWhite
plt.rcParams['figure.facecolor'] = startBlack
plt.rcParams['figure.edgecolor'] = startBlack
plt.rcParams['xtick.color'] = startWhite
plt.rcParams['ytick.color'] = startWhite
plt.rcParams['text.color'] = startWhite
fig.canvas.draw_idle()
return fig,
noFrames = 50
# Create the animation
ani = animation.FuncAnimation(fig, update1, frames=range(noFrames*5), blit=False, repeat=False)
ani.event_source.stop() #stop the looping
# Save the animation as a GIF
ani.save('TEST01_fade.gif', writer='pillow', fps=10)
plt.close()
Подробнее здесь: https://stackoverflow.com/questions/784 ... imated-gif