Qutaiba
Qutaiba

Reputation: 145

continuous 3d plotting using matplotlib

I am working on a project that require a continuous 3D plotting using matplotlib. the whole project is complex, but i summarized the problem with this simple example:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import time


count=0
fig = plt.figure()
ax = fig.gca(projection='3d')
z = [0]
x = [0]
y = [0]
plt.show()
while True:
    count +=1
    x.append(count)
    x.append(count)
    x.append(count)
    ax.plot(x, y, z)
    time.sleep(1)
    plt.draw()

in this code, I am trying to redraw the 3D line with new x,y,z values. but nothing happens!

Upvotes: 2

Views: 1746

Answers (1)

heltonbiker
heltonbiker

Reputation: 27615

I don't know why it didn't work the way you coded, but I could get it to work like this:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import time
import numpy


count=0
fig = plt.figure()
ax = fig.gca(projection='3d')
z = [0]
x = [0]
y = [0]

plt.ion()    ###

plt.show()
while True:
    count +=1
    x.append(count)
    y.append(count**2) #
    z.append(count**3) # just for eye-candy

    ax.plot(numpy.array(x),    ###
            numpy.array(y),    ###
            numpy.array(z))    ###

    time.sleep(1)
    plt.draw()

The ion part makes sense, but converting your sequences to arrays was necessary here, because I was getting an error due to (apparently) matrix multiplication being impossible with simple sequences:

(...)
File "C:\Python27\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py", line     158, in proj_transform_vec
    vecw = np.dot(M, vec)
TypeError: can't multiply sequence by non-int of type 'float'

Upvotes: 1

Related Questions