I feel like I must be missing something extremely simple. I'm trying to graph a 3D matrix in a scatterplot. This is what I have
import numpy as np
import matplotlib.pyplot as plt
from pylab import plot
from mpl_toolkits.mplot3d import Axes3D as axes
from matplotlib import cm
mat = np.matrix([ [245, 10, 3], [245, 7, 3], [246, 10, 3] ])
matt = mat.T
x = matt[0]
y = matt[1]
z = matt[2]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.scatter(x, y, z, zdir='z', c = 'red')
plt.show()
Simple, right? But I keep getting the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
self.show()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 248, in draw
for col in self.collections]
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/art3d.py", line 319, in do_3d_projection
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/proj3d.py", line 203, in proj_transform_clip
return proj_transform_vec_clip(vec, M)
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/proj3d.py", line 160, in proj_transform_vec_clip
vecw = np.dot(M, vec)
TypeError: can't multiply sequence by non-int of type 'float'
which I'm not sure how to fix. Where am I going wrong? The code is pretty similar to this, so maybe I'm cutting the matrix wrong or something? This doesn't address the problem either. Any ideas?
Edit: Okay, I ended up just re-slicing it the old fashioned way via:
sx, sy = mat.shape
for i in range(0, sx):
x.append(mat[i,0])
y.append(mat[i,1])
z.append(mat[i,2])
Which is probably not the best way but I got annoyed. And it works, so there's that.