程式描述:我有一部手機每 2 秒向服務器發送一次陀螺儀資料。我每秒都從服務器檢索資料。我的程式的目的是讀取這個資料流并使用可以自動更新的 matplotlib 進行 3d 可視化。
問題:雖然得到 x,y,z 坐標,但繪圖顯示空白 3D 空間。我一直試圖弄清楚為什么和如何,但無濟于事。非常感謝任何幫助。提前致謝。
代碼:
from requests import get
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
ip_adress = 'http://xxxx'
port = ':xxxx'
endpoint = '/xxxx/'
ax = plt.axes(projection='3d')
def animate(i):
collected_data = get(ip_adress port endpoint).json()['locators']['sensors'][0]
x = np.linspace(0, collected_data['value0'], 100)
y = np.linspace(0, collected_data['value1'], 100)
z = np.linspace(0, collected_data['value2'], 100)
ax.plot3D(x, y, z)
plt.cla()
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.tight_layout()
plt.show()
輸出:

uj5u.com熱心網友回復:
問題是,對于每次迭代,您都添加一條線并立即使用plt.clar().
您需要做的是初始化一個空行,然后在animate函式中更新點:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
ax = plt.axes(projection='3d')
line, = ax.plot3D([], [], [])
def animate(i):
collected_data = get(ip_adress port endpoint).json()['locators']['sensors'][0]
x = np.linspace(0, collected_data['value0'], 100)
y = np.linspace(0, collected_data['value1'], 100)
z = np.linspace(0, collected_data['value2'], 100)
line.set_data_3d(x, y, z)
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.tight_layout()
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459070.html
標籤:Python matplotlib 动画片 3d
下一篇:出現時禁用SwiftUI幀影片
