[英]Updating line in 3D plot Animation
我正在嘗試 plot 中的一行 3D animation。 我無法擦除我在 update_line 中創建的圖。 問題是我總是添加新行。 我看到的最佳解決方案是在 function 之外創建行並更新這些行的數據,但我似乎無法在 3D 中找到 plot 的方法
第一次發帖,僅僅幾個小時的 python。 對 canvas 一無所知。 請保持簡單
import matplotlib; matplotlib.use("TkAgg")
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
dotinline = 101
nframe = 7
fig = plt.figure(figsize=(16, 8))
ax = p3.Axes3D(fig)
ax.view_init(0, 90)
def init():
ax.plot([-10, 10], [0, 0], [0, 0], "k")
ax.plot([0, 0], [-10, 10], [0, 0], "k")
ax.plot([0, 0], [0, 0], [-10, 10], "k")
ax.plot([], [], [], "b")
def update_lines(index):
seg = []
x = np.linspace(-10, 10, dotinline)
power = x**(-(nframe-1)/2+index)
for i in range(len(x)-1):
a = [x[i+1], x[i]]
b = [0, 0]
c = [power[i+1], power[i]]
seg.append([a, b, c])
for data in seg:
ax.plot3D(data[0], data[1], data[2], "-b")
ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')
line_ani = animation.FuncAnimation(fig, update_lines, init_func=init, frames=nframe, interval=2000, blit=False)
plt.show()
檢查此代碼:
import matplotlib; matplotlib.use("TkAgg")
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
dotinline = 101
nframe = 7
fig = plt.figure(figsize=(16, 8))
ax = p3.Axes3D(fig)
ax.view_init(0, 90)
def init():
ax.plot([-10, 10], [0, 0], [0, 0], "k")
ax.plot([0, 0], [-10, 10], [0, 0], "k")
ax.plot([0, 0], [0, 0], [-10, 10], "k")
ax.plot([], [], [], "b")
def update_lines(index):
ax.cla()
init()
seg = []
x = np.linspace(-10, 10, dotinline)
power = x**(-(nframe-1)/2+index)
for i in range(len(x)-1):
a = [x[i+1], x[i]]
b = [0, 0]
c = [power[i+1], power[i]]
seg.append([a, b, c])
for data in seg:
ax.plot3D(data[0], data[1], data[2], "-b")
ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')
line_ani = animation.FuncAnimation(fig, update_lines, init_func=init, frames=nframe, interval=2000, blit=False)
plt.show()
要刪除前面的行,只需添加ax.cla()
。
但是,這也擦除了黑線軸; 因此,在ax.cla()
之后,我再次運行init()
,以重新繪制軸線。
最后,最好移動塊:
ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')
在update_lines()
function 中,以便在刪除行時保留這些選項。
使用此代碼,我得到以下 animation:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.