有一個簡單的代碼,根據一些簡單的規則來畫線。
它可以作業,但我想通過改變一個引數theta_time來使它變得生動
importmatplotlib.pyplot as plt
import numpy as np
from numpy import *
import matplotlib.animation as animation
List_1 = [6,4,2, 1]
List_2 = [0,0,0, 0]
List_3 = [6,4,3, 2]
圖,ax = plt.subplots()
C_x = 0 0] fig, ax =plt.subplots()
Cx = [0]
cy = [0]
theta_time = 1 #I want to update this value from 0 to 10for freq,amp,phase in zip(List_3,List_1, List_2)。
C_x = amp*np.cos(freq * theta_time np.deg2rad(phase))
C_y = amp*np.sin(freq * theta_time np.deg2rad(phase))
cx.append(C_x)
cy.append(C_y)
plt.scatter(cx,cy)
plt.plot(cx, cy, '.r-',lineewidth=1)
plt.show()
我試著添加下面的代碼來制作影片,但它并不奏效
def animations(theta_time)。
for freq,amp, phase in zip(List_3,List_1, List_2):
C_x = amp*np.cos(freq * theta_time np.deg2rad(phase))
C_y = amp*np.sin(freq * theta_time np.deg2rad(phase))
cx.append(C_x)
cy.append(C_y)
ani = animation.FuncAnimation(
fig, animations,frames=np.range(0,1,0.01),interval=10, blit=False)
uj5u.com熱心網友回復:
你應該在animation函式中移動所有的計算和繪圖線。在這個函式中要做的第一件事是用ax.cla()擦除以前的繪圖(否則新的幀會與以前的重疊)。我建議在animation中添加ax.set_xlim和ax.set_ylim,以便在每一幀中固定軸的限制,避免不愉快的軸在每一幀中重新調整大小。我還添加了一個帶有當前theta_time值的標題,如果你想的話,你可以將其洗掉。
完整的代碼
importmatplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
List_1 = [6,4,2, 1]
List_2 = [0,0,0, 0]
List_3 = [6,4,3,2]
def animate(theta_time)。
ax.cla()
C_x = 0: ax.cla()
C_y = 0 0
Cx = [0]
cy = [0]
for freq,amp,phase in zip(List_3,List_1, List_2)。
C_x = amp*np.cos(freq * theta_time np.deg2rad(phase))
C_y = amp*np.sin(freq * theta_time np.deg2rad(phase))
cx.append(C_x)
cy.append(C_y)
ax.scatter(cx,cy)
ax.plot(cx, cy, '.r-',lineewidth=1)
ax.set_xlim(-15, 15)
ax.set_ylim(-15, 15)
ax.set_title(r'$ heta_{time} = $' str(theta_time))
fig, ax = plt.subplots(figsize = (5, 5)
ani = FuncAnimation(fig = fig, func = animate, interval = 500, frames = 10)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/319398.html
標籤:


