0

I have searched for this in google, but found solutions for 2d points in real time.How can I achieve this for stream of 3d point.

Here I should be able to add new points to plot.

I tried this, its just plots series of data. How to update?

djkpA
  • 1,224
  • 2
  • 27
  • 57

1 Answers1

1

You could just plot in interactive mode, for example the following keeps adding new points,

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

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

plt.ion()
plt.show()

x = np.linspace(0.,np.pi*4.,100)
ax.set_xlim([0.,13.])
ax.set_ylim([-1.5,1.5])
ax.set_zlim([-1.5,1.5])
for i in x:
    ax.scatter(i, np.sin(i), np.cos(i))
    print(i)
    plt.pause(0.01)

UPDATE: added example of labelling

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

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

plt.ion()
plt.show()

lsp = np.linspace(0.,np.pi*4.,100)
ax.set_xlim([0.,13.])
ax.set_ylim([-1.5,1.5])
ax.set_zlim([-1.5,1.5])
for i, x in enumerate(lsp):
    y = np.sin(x)
    z = np.cos(x)
    ax.scatter(x, y, z)
    if i%10 == 0:
        ax.text(x, y, z, str(np.round(x,3))+", "
                        +str(np.round(y,3))+", "
                        +str(np.round(z,3)))
    plt.pause(0.01)
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • Its working Thanks :). Is there a way to display a 3d data of point on hover or click on a point. Currently on bottom of fig it showing some error. – djkpA May 10 '17 at 08:48
  • Strange you get an error, works okay for me (`matplotlib 1.4.3`) but I don't think you can easily work out which point you are selecting in 3D anyway... You could add `ax.text` to label points, I'd edited example to plot every 10th points coords but it quickly gets messy. – Ed Smith May 10 '17 at 09:41
  • Plotting is very slow. Its not real time. How to optimize it to make real time. I get new point for every 0.02 seconds @Ed Smith – djkpA May 10 '17 at 13:25
  • Yeah, matplotlib is generally slow (focused on publication figures) and `scatter` is only fast if you plot all as a single collection (lots of points together). Doing this individually is never going to be fast, you could group sets of points and plot all of these intermittently? Animation may be faster maybe with blitting (see http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib?noredirect=1&lq=1) but plot doesn't seem to have a way to set data in 3D. Ideally you append to one collection but doesn't seem to be an interface for this. – Ed Smith May 10 '17 at 17:43
  • is there any other libraries for plotting which are particularly designed for real time. – djkpA May 10 '17 at 17:50
  • Sorry for the slow reply, the only other one I've used is mayavi, good for 3D but don't think that'll be faster. Might be worth asking a new question on this. – Ed Smith May 14 '17 at 17:05