如何把这个情节变成动画?我有一个情节,但想以某种方式将其变成动画
How to turn this plot into an animation? I have a plot but would like to turn this into an animation somehow
过去几个晚上我一直在努力如何在每个时间步长或每个 x 步长之后将我在图中的波浪变成某种动画。我如何修改和编写代码,以便我的程序以某种方式在波浪的每个时间步动画。我对 python 和编程还很陌生,以前从未使用过 matplotlib 的动画部分。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
T = 20.0 # t
X = 10.0 # x
n = 300
m = 300
#positions of time and space, xp= x position accross grid, tp = t position accross grid
tp = T / (n - 1)
xp = X / (m - 1)
C = 0.5
U = np.zeros((n, m))
# Set the initial values for each x position
for i in range(0, n):
U[i, 0] = np.exp(- (i * xp - 2.5) ** 2)
for i in range(1, n): # across x
for j in range(1, m): # across t
U[i, j] = (xp * U[i, j - 1] + C * tp * U[i - 1, j]) / (xp + C * tp) # equation for new distribution value
fig = plt.figure(1)
#gives time position instead of time step
tn = np.zeros((m, 1))
for j in range(0, m):
tn[j] = j * tp
#gives x position instead of x step
xn = np.zeros((n, 1))
for j in range(0, n):
xn[j] = j * xp
for i in [0, 50, 100, 150, 200, 250, 299 ]: # selects which position of time
label = 't = ' + str(tn[i][0]) # lables legend
subfig = fig.add_subplot(1, 1, 1)
subfig.plot(xn, U[:, i], label=label)
subfig.legend()
subfig.grid(True)
print(tn)
# Save Image
plt.xlabel('x: position')
plt.ylabel('u: u(x, t)')
plt.title(r'$\frac{\partial u}{\partial t} + C \frac{\partial u}{\partial x} = 0$')
plt.savefig('transport-equation')`
plt 是一个很难适应的包。一般来说,图形不是一件容易管理的事情,plt 试图尽可能简单,同时提供最大的灵活性。一般来说,当你使用plt时,有很多全局变量是自动为你生成、更新、清理和处理的。当您使用东西 "plt.xlabel" 时,您实际上是将它应用到特定图形中的特定轴,这是自动为您确定的。如果你想在 plt and/or 中进行更多控制,你想做一些像动画这样复杂的事情,那么你需要让你的全局变量明确。
#Create xn and U.
import matplotlib.pyplot as plt
figure = plt.figure() #This is the window that pops open.
axis = figure.add_subplot(1,1,1) #This is a graph/grid.
axis.grid(True) #Add a grid to the axis.
label = 't = ' + str(tn[i][0])
plots = axis.plot(xn,U[:,0],label=label) #These are the plots of data with X and Y.
X 和 Y 数组一次可以生成多个图,因此,图是一个包含一项的列表。要了解这是如何工作的,您可以实时操作数据并观察它在 plt window 中的变化。
figure.show()
plots[0].set_ydata(U[:,10])
plots[0].set_ydata(U[:,-1])
# Close the window when done.
要制作动画,我们需要告诉 plt 将动画应用于给定的图形。然后 plt 将尝试更新图形和已附加到它的所有内容。如果你已经打开了 window,动画仍然会被应用和工作,但是你也会保留图中最初绘制的任何东西(所以我们应该关闭 window 并重新编码动画). plt 不遵循一次执行一行与一次执行所有行相同的约定。 plt 在打开 window 前后表现不同。
#Create xn and U.
import matplotlib.pyplot as plt
figure = plt.figure()
axis = figure.add_subplot(1,1,1)
axis.grid(True)
label = 't = ' + str(tn[i][0])
plots = axis.plot(xn,U[:,0],label=label)
def animate_function(frame):
frame %= 300 #frame is an integer that counts from 0.
plots[0].set_ydata(U[:,frame]) #Change which globals you want.
return plots #Return the changed items so plt knows.
#Tell plt to apply this animation function to your figure.
#Tell plt to wait approximately 10ms per frame.
#Tell plt to only update pixels that actually change (called blit).
#Save to a global variable so plt doesn't get upset at you.
ani = animation.FuncAnimation(figure,animate_function,interval=10,blit=True)
#Now open the window and show the figure.
figure.show()
过去几个晚上我一直在努力如何在每个时间步长或每个 x 步长之后将我在图中的波浪变成某种动画。我如何修改和编写代码,以便我的程序以某种方式在波浪的每个时间步动画。我对 python 和编程还很陌生,以前从未使用过 matplotlib 的动画部分。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
T = 20.0 # t
X = 10.0 # x
n = 300
m = 300
#positions of time and space, xp= x position accross grid, tp = t position accross grid
tp = T / (n - 1)
xp = X / (m - 1)
C = 0.5
U = np.zeros((n, m))
# Set the initial values for each x position
for i in range(0, n):
U[i, 0] = np.exp(- (i * xp - 2.5) ** 2)
for i in range(1, n): # across x
for j in range(1, m): # across t
U[i, j] = (xp * U[i, j - 1] + C * tp * U[i - 1, j]) / (xp + C * tp) # equation for new distribution value
fig = plt.figure(1)
#gives time position instead of time step
tn = np.zeros((m, 1))
for j in range(0, m):
tn[j] = j * tp
#gives x position instead of x step
xn = np.zeros((n, 1))
for j in range(0, n):
xn[j] = j * xp
for i in [0, 50, 100, 150, 200, 250, 299 ]: # selects which position of time
label = 't = ' + str(tn[i][0]) # lables legend
subfig = fig.add_subplot(1, 1, 1)
subfig.plot(xn, U[:, i], label=label)
subfig.legend()
subfig.grid(True)
print(tn)
# Save Image
plt.xlabel('x: position')
plt.ylabel('u: u(x, t)')
plt.title(r'$\frac{\partial u}{\partial t} + C \frac{\partial u}{\partial x} = 0$')
plt.savefig('transport-equation')`
plt 是一个很难适应的包。一般来说,图形不是一件容易管理的事情,plt 试图尽可能简单,同时提供最大的灵活性。一般来说,当你使用plt时,有很多全局变量是自动为你生成、更新、清理和处理的。当您使用东西 "plt.xlabel" 时,您实际上是将它应用到特定图形中的特定轴,这是自动为您确定的。如果你想在 plt and/or 中进行更多控制,你想做一些像动画这样复杂的事情,那么你需要让你的全局变量明确。
#Create xn and U.
import matplotlib.pyplot as plt
figure = plt.figure() #This is the window that pops open.
axis = figure.add_subplot(1,1,1) #This is a graph/grid.
axis.grid(True) #Add a grid to the axis.
label = 't = ' + str(tn[i][0])
plots = axis.plot(xn,U[:,0],label=label) #These are the plots of data with X and Y.
X 和 Y 数组一次可以生成多个图,因此,图是一个包含一项的列表。要了解这是如何工作的,您可以实时操作数据并观察它在 plt window 中的变化。
figure.show()
plots[0].set_ydata(U[:,10])
plots[0].set_ydata(U[:,-1])
# Close the window when done.
要制作动画,我们需要告诉 plt 将动画应用于给定的图形。然后 plt 将尝试更新图形和已附加到它的所有内容。如果你已经打开了 window,动画仍然会被应用和工作,但是你也会保留图中最初绘制的任何东西(所以我们应该关闭 window 并重新编码动画). plt 不遵循一次执行一行与一次执行所有行相同的约定。 plt 在打开 window 前后表现不同。
#Create xn and U.
import matplotlib.pyplot as plt
figure = plt.figure()
axis = figure.add_subplot(1,1,1)
axis.grid(True)
label = 't = ' + str(tn[i][0])
plots = axis.plot(xn,U[:,0],label=label)
def animate_function(frame):
frame %= 300 #frame is an integer that counts from 0.
plots[0].set_ydata(U[:,frame]) #Change which globals you want.
return plots #Return the changed items so plt knows.
#Tell plt to apply this animation function to your figure.
#Tell plt to wait approximately 10ms per frame.
#Tell plt to only update pixels that actually change (called blit).
#Save to a global variable so plt doesn't get upset at you.
ani = animation.FuncAnimation(figure,animate_function,interval=10,blit=True)
#Now open the window and show the figure.
figure.show()