[英]How to Animate Text in 3D Scatter Plot?
我想制作一個3D散點圖動畫,其中每個數據點都有一個隨其移動的文本標簽。
現在,文本標簽確實按照我的意願跟隨數據點,但是它們在每一幀中都保持不變; 文本不會在上一次動畫更新中消失。 參見下圖。 數據點本身運行良好。
2個問題的答案可能對我有幫助>
這是繪圖功能 :
def graph(data, update_cnt):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
dots = ax.scatter(data[0][0][:], data[1][0][:], data[2][0][:])
dot_txt = nmp.ones(nmp.size(data,2), dtype=str)
for n in range(0,nmp.size(data, 2)):
dot_txt[n] = ax.text(data[0][0][n], data[1][0][n], data[2][0][n],'%s'%(n))
ani = animation.FuncAnimation(fig, update, update_cnt, fargs=(dots, data, dot_txt, ax), interval=300)
plt.show()
以及動畫更新功能 :
def update(num, dots, data, dot_txt, ax):
y = data[0][num][:]
x = data[1][num][:]
z = data[2][num][:]
dots._offsets3d = (x, y, z)
#dot_txt._something_to_update_text_here()
dot_txt = nmp.ones(nmp.size(data,2), dtype=str)
for n in range(0,nmp.size(data, 2)):
dot_txt[n] = ax.text(data[1][num][n], data[0][num][n], data[2][num][n],'%s'%(n))
我找到了解決方案。
我認為必須指出,我無法對3D動畫散點圖使用更常見的解決方案,因為我需要針對不同點使用不同的標記樣式。 這迫使我迭代繪制每個散點,而不是將列表傳遞給更新函數。 但是,這樣做可以很好地解決文本動畫問題。
frame_list包含每個幀中每個數據點的x,y,z坐標和樣式。
def graph(frame_list):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
frame_cnt = len(frame_list)
ani = animation.FuncAnimation(fig, update_graph, frame_cnt,
fargs=(frame_list, ax, frame_cnt), interval=600)
plt.show()
成功的最大原因是每幀之前的ax.clear()調用。
def update_graph(f, frame_list, ax, cnt):
ax.clear()
f = nmp.mod(f, cnt)
frame = frame_list[f]
for n in range(len(frame.marker)):
x, y, z, s, c, m, name = frame.get_params(n)
ax.scatter(x, y, z, s=s, c=c, marker=m)
ax.text(x, y, z, '%s'%(name))
get_params函數返回該幀的所有相關數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.