【发布时间】:2020-05-17 17:15:20
【问题描述】:
谁能告诉我我的动画代码有什么问题?
我编写了这个程序,它将生成 40 张 png 图像和一个动画 gif 图像。
动画应该是一个被切成 5 段的球体,这些段会左右移动,但是,正如您所看到的,它并没有按我的计划工作
(我只发布了 12 帧的 gif,因为 40 帧会太大)
有人可以告诉我如何更正吗?
import matplotlib.pyplot as plt
from numpy import sin,cos,pi,outer,ones,size,linspace
from mpl_toolkits.mplot3d import axes3d
# Define the x, y, and z lists for the sphere:
x = 10*outer(cos(linspace(0, 2*pi)), sin(linspace(0, pi)))
y = 10*outer(sin(linspace(0, 2*pi)), sin(linspace(0, pi)))
z = 10*outer(ones(size(linspace(0, 2*pi))), cos(linspace(0, pi)))
for n in range(40):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, color = ('r'))
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
ax.set_zlim(-10,10)
plt.savefig(str(n)+'.png') #Save the image into a numbered png file
print(str(n)+'.png completed.')
plt.close()
sign = 1
for count in range(5): #Slice the sphere into 5 segments
for num in range(len(z)//5*count,len(z)//5*(count+1)):
z[num] += sign # Make the segments go positive and negative directions
sign *= -1
from PIL import Image
images = []
# Open each png file and store in images list
for n in range(40):
exec('a'+str(n)+'=Image.open("'+str(n)+'.png")')
images.append(eval('a'+str(n)))
# Create an animated gif file:
images[0].save('ball.gif',
save_all=True,
append_images=images[1:],
duration=100,
loop=0)
【问题讨论】:
-
请不要进行完全改变问题所问内容的编辑。相反,创建一个新问题。否则面临同样问题的人将无法找到您的问题和提供的答案。
-
@f9c69e9781fa194211448473495534 编辑似乎是对问题的澄清和更好的表述,以及一些小的代码更正。
-
@JohanC 最初的问题是关于为什么动画没有移动。现在动画正在移动,问题是如何让它以特定的方式移动。这使下面给出的答案无效。如果我花时间回答一个问题,然后对该问题进行编辑,使我的答案不再适用,我会感到沮丧。
标签: python matplotlib animation python-imaging-library animated-gif