2

How do I modify the xyz data of a 3d scatter plot in matplotlib for fast on-line animations? In other words where do matplotlib patchcollection3d objects save the xyz coordinates, and how do I set them? For example:

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

## generate some random data
pts = np.random.uniform(0,10,(10,20,30))

plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
patch_collection_instance = ax.scatter(pts[:,0],pts[:,1],pts[:,2], c='m', marker='o')

What do I do next with patch_collection_instance if, for example, I want to translate all points by a random amount?

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
Hennadii Madan
  • 1,573
  • 1
  • 14
  • 30

1 Answers1

3

The coordinates are stored in the attribute _offsets3d. While there is a get_offsets() method and a set_offsets() method, those appear to be inherited from the 2d version and don't work properly for 3d. _offsets3d contains a tuple of x, y, and z coordinate tuples. Let's say you want to shift every point by 10 in the x direction. You'd add 10 to every number in the x-coordinate tuple, then set the _offsets3d to the new tuple.

I am not sure if this is faster than just clearing the figure and calling scatter again with new coordinates, which should have the same effect.

Example code:

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

## generate some random data
pts = np.random.uniform(0,10,(10,20,30))

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
patch_collection_instance = ax.scatter(pts[:,0],pts[:,1], pts[:,2], c='m', marker='o')
x, y, z = patch_collection_instance._offsets3d
print x
x = [i + 10 for i in x]
offsets = (x, y, z)
patches2 = copy(patch_collection_instance)
patches2._offsets3d = offsets
patches2._facecolor3d = [[0, 0, 1, 1]]
ax.add_collection3d(patches2)
plt.xlim(0, 20)
plt.show()

3d Scatter Plot

Amy Teegarden
  • 3,842
  • 20
  • 23
  • I mark this as correct answer , however I did not check myself. I've finished that project short on time to play around right now. Thanks. – Hennadii Madan Sep 08 '15 at 11:08