我有以下創建圖形影片的代碼。該圖應該從 0 開始,但第一個區間圖沒有出現。
下面是代碼:
import matplotlib.pylab as plt
import matplotlib.animation as animation
import numpy as np
fig, ax = plt.subplots()
left = -1
right = 2*np.pi - 1
def animate(i):
global left, right
left = left 1
right = right 1
x = np.linspace(left, right, 50)
y = np.cos(x)
ax.cla()
ax.set_xlim(left, right)
ax.plot(x, y, lw=2)
ani = animation.FuncAnimation(fig, animate, interval = 1000)
plt.show()
對于第一個區間 [0, 2π],圖表不會出現。有什么錯誤?
uj5u.com熱心網友回復:
我改變了一點你的代碼:
- 首先,我在
animate函式外部繪制第一幀并從中生成一個line物件 - 然后我更新函式
line內的資料animate - 我建議使用
i計數器(從每幀開始0并增加1)來更新您的資料,而不是呼叫global變數并更改它們
完整代碼
import matplotlib.pylab as plt
import matplotlib.animation as animation
import numpy as np
fig, ax = plt.subplots()
left = 0
right = 2*np.pi
x = np.linspace(left, right, 50)
y = np.cos(x)
line, = ax.plot(x, y)
ax.set_xlim(left, right)
def animate(i):
x = np.linspace(left i, right i, 50)
y = np.cos(x)
line.set_data(x, y)
ax.set_xlim(left i, right i)
return line,
ani = animation.FuncAnimation(fig = fig, func = animate, interval = 1000)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506583.html
標籤:Python matplotlib 动画 数据可视化 可视化
上一篇:Flutter:當寬度基于螢屏尺寸時,如何為小部件的寬度設定影片?
下一篇:如何重置和重復CSS影片?
