2017-12-16 5 views
0

私は、リアルタイムで(x、y、z)で追加されたmatplotlib(python)を使用して3Dでプロットしたいと考えています。Matplotlibをz軸に追加

データはx軸とy軸に正常に追加されますが、z軸で問題が発生しました。matplotlibのドキュメントを検索しても、解決策は見つかりませんでした。

z軸にデータを追加するために、このコードに追加/変更するものを教えてください。

何が正しく動作:

return plt.plot(x, y, color='g') 

問題:

return plt.plot(x, y, z, color='g') 

コード:

from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.animation as animation 
import random 

np.set_printoptions(threshold=np.inf) 
fig = plt.figure() 
ax1 = fig.add_subplot(111, projection='3d') 


x = [] 
y = [] 
z = [] 
def animate(i): 
    x.append(random.randint(0,5)) 
    y.append(random.randint(0,5)) 
    z.append(random.randint(0,5)) 

    return plt.plot(x, y, color='g') 
    #return plt.plot(x, y, z, color='g') => error 


ani = animation.FuncAnimation(fig, animate, interval=1000) 
ax1.set_xlabel('x') 
ax1.set_ylabel('y') 
ax1.set_zlabel('z') 
plt.show() 

これが正しく行わ取得する方法を?

答えて

0

3Dプロットに使用するプロット方法は、Axes3Dのプロット方法です。したがって、あなたは

ax1.plot(x, y, z) 

をプロットする必要があるしかし、あなたではなく(それは全てのプロットで構成されるように、ラインの外観は、何らかの形で、ラスタライズする)すべての繰り返しそれをプロットのデータを更新したいようです。

したがって、set_dataと3次元のset_3d_propertiesを使用できます。プロットを更新すると、次のようになります。

from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.animation as animation 

fig = plt.figure() 
ax1 = fig.add_subplot(111, projection='3d') 

x = [] 
y = [] 
z = [] 

line, = ax1.plot(x,y,z) 

def animate(i): 
    x.append(np.random.randint(0,5)) 
    y.append(np.random.randint(0,5)) 
    z.append(np.random.randint(0,5)) 
    line.set_data(x, y) 
    line.set_3d_properties(z) 


ani = animation.FuncAnimation(fig, animate, interval=1000) 
ax1.set_xlabel('x') 
ax1.set_ylabel('y') 
ax1.set_zlabel('z') 
ax1.set_xlim(0,5) 
ax1.set_ylim(0,5) 
ax1.set_zlim(0,5) 
plt.show() 
+0

ありがとう、awesome – shadow

関連する問題