Reputation: 23
I want to create an animated scatter plot in 3D, where the latest point is shown in a certain colour and then grays out, as far as the next point is generated.
The input file 'coordinates.txt' simply includes the x, y and z values in three columns.
The code works, as long as I skip the z value and just use a 2d projection. When I am adding the z coordinate I get a TypeError: object of type 'float' has no len()
.
I don't understand why I get this error for the 3d but not for the 2d projection and why it wants to calculate len() in the 3d version.
I would be happy, if somebody could help me to figure this out!
input_file = open('coordinates.txt', 'r').readlines()
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
pylab.xlim([0,6])
pylab.ylim([0,6])
ax.set_zlim([0,6])
line = None
def main(i):
global line
if line is not None:
line.set_color('gray')
line, = ax.plot(x,y,z, 'ro')
for row in input_file:
el = row.split()
x = float(el[0])
y = float(el[1])
z = float(el[2])
plt.ion()
plt.show()
ani = animation.FuncAnimation(fig, main)
plt.draw()
Here the entire traceback:
Traceback (most recent call last):
File "./def_func_3d.py", line 65, in
ani = animation.FuncAnimation(fig, main)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 1010, in init
TimedAnimation.init(self, fig, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 864, in init
*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 545, in init
self._init_draw()
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 1035, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 1049, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "./def_func_3d.py", line 43, in main
line, = ax.plot(x,y,z, 'ro')
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 1511, in plot
zs = np.ones(len(xs)) * zs
TypeError: object of type 'float' has no len()
Upvotes: 2
Views: 962
Reputation: 5659
plot
is expecting iterables for x
and y
, which it uses to size z
if it is a scalar. It should be fine if you change the code to
line, = ax.plot([x], [y], z, 'ro')
Upvotes: 1