0

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.

Community
  • 1
  • 1
Matter Cat
  • 1,538
  • 1
  • 14
  • 23
  • 1
    You should put the complete traceback, not just a small part of it. – Anand S Kumar Oct 11 '15 at 04:31
  • Edited in. Is it a problem with my version of python/matplot lib? – Matter Cat Oct 11 '15 at 04:33
  • If you solved the problem, please put it as an *answer*, not as an addition to the question. And then mark it as solved (you can answer and mark your own answers). That way, it's easy to the problem has a solution. –  Oct 11 '15 at 04:57
  • 1
    I think that if you replace `np.matrix` by `np.array`, you may get what you want. Matrices behave differently than (2-D) arrays in numpy, and the slicing you're liking for won't work this way. –  Oct 11 '15 at 05:01
  • Well, I would prefer to find a method for slicing it DIRECTLY instead of having to do what I suspect is not the Python Approved Answer (TM). That's why I didn't put it as an answer. I will change it later if nothing else pops up, though. – Matter Cat Oct 11 '15 at 05:09
  • And also, your solution turned out to be what I need, so thanks! Put it as an answer and I'll accept it. – Matter Cat Oct 11 '15 at 05:11

0 Answers0