有人能告诉我我的动画代码有什么问题吗?
Can someone tell me what's wrong with my animation code?
有人能告诉我我的动画代码有什么问题吗?
我编写了这个程序,它将生成 40 张 png 图像和一个动画 gif 图像。
动画应该是一个被切成 5 的球体片段,片段左右移动,但是,正如您将看到的,它并没有像我计划的那样真正起作用
(我只 posted 12 帧的 gif,因为 40 帧会太大 post)
谁能告诉我如何更正它?
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)
问题是您对所有线段使用相同的数组,因此绘图将在某些顶点处保持连接。这就是绘图功能的工作原理,它不知道你想分开部分。
你必须split之前的数组,然后单独修改部分。
充其量也是为 x
和 y
完成的,这样您就不必 fiddle 使用索引和烦人的内部 for 循环 :)
我为你准备了一些开始的东西:
import matplotlib.pyplot as plt
from numpy import sin,cos,pi,outer,ones,size,linspace
from mpl_toolkits.mplot3d import axes3d
import numpy as np
# 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)))
def split_and_array(a):
return np.array(np.split(a, 5, axis=0))
x = split_and_array(x)
y = split_and_array(y)
z = split_and_array(z)
for n in range(40):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
for k in range(5):
ax.plot_surface(x[k], y[k], z[k], 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
z[count] += sign # Make the segments go positive and negative directions
sign *= -1
有人能告诉我我的动画代码有什么问题吗?
我编写了这个程序,它将生成 40 张 png 图像和一个动画 gif 图像。
动画应该是一个被切成 5 的球体片段,片段左右移动,但是,正如您将看到的,它并没有像我计划的那样真正起作用
(我只 posted 12 帧的 gif,因为 40 帧会太大 post)
谁能告诉我如何更正它?
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)
问题是您对所有线段使用相同的数组,因此绘图将在某些顶点处保持连接。这就是绘图功能的工作原理,它不知道你想分开部分。
你必须split之前的数组,然后单独修改部分。
充其量也是为 x
和 y
完成的,这样您就不必 fiddle 使用索引和烦人的内部 for 循环 :)
我为你准备了一些开始的东西:
import matplotlib.pyplot as plt
from numpy import sin,cos,pi,outer,ones,size,linspace
from mpl_toolkits.mplot3d import axes3d
import numpy as np
# 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)))
def split_and_array(a):
return np.array(np.split(a, 5, axis=0))
x = split_and_array(x)
y = split_and_array(y)
z = split_and_array(z)
for n in range(40):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
for k in range(5):
ax.plot_surface(x[k], y[k], z[k], 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
z[count] += sign # Make the segments go positive and negative directions
sign *= -1