0

So I have a 3D graph that is a scatterplot updating a point by going through a dataframe. I have it add a new point ever .1 seconds. Here is my code:

ion()
fig = figure()
ax = fig.add_subplot(111, projection='3d')
count = 0
plotting = True
while plotting:
    df2 = df.ix[count]
    count += 1
    xs = df2['x.mean']
    ys = df2['y.mean']
    zs = df2['z.mean']
    t = df2['time']
    ax.scatter(xs, ys, zs)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title(t)
    draw()
    pause(0.01)
    if count > 50:
        plotting = False
ioff()
show()

How can I get it to only show the new point on the live-updated graph. Right now it starts with one point and then adds another and another until there is a total of 50 points on the graph.

So what I want is for there never to be more than one point on the graph and just have that point be changing as it iterates through. How can I do this?

Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128

1 Answers1

2
ion()
fig = figure()
ax = fig.add_subplot(111, projection='3d')
count = 0
plotting = True
# doesn't need to be in loop
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

lin = None
while plotting:
    df2 = df.ix[count]
    count += 1
    xs = df2['x.mean']
    ys = df2['y.mean']
    zs = df2['z.mean']
    t = df2['time']
    if lin is not None:
        lin.remove()
    lin = ax.scatter(xs, ys, zs)
    ax.set_title(t)
    draw()
    pause(0.01)
    if count > 50:
        plotting = False
ioff()
show()

In principle you can also use a normal plot instead of scatter and update the data in the loop, but the 3D updating can be wonky/may require poking at the internals a bit.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • for the record, `ax.set_title(t)` does need to be in the loop because it lets me keep track of time. Now the point is always in the middle and the axis labels change when I do this...how do I prevent that? – Ryan Saxe May 08 '13 at 20:02
  • @RyanSaxe opps, sorry, didn't fully read what it was doing. Restored that bit. – tacaswell May 08 '13 at 20:50
  • sorry about that, there was one issue with this so I didn't accept it although it did work for the question. I posted a new question [here](http://stackoverflow.com/questions/16468538/matplotlib-3d-plot-with-changing-labels) – Ryan Saxe May 09 '13 at 18:46