在Python中绘制3D矩阵 - TypeError:不能将序列乘以'float'类型的非int

时间:2015-10-11 04:30:40

标签: python

我觉得我必须遗漏一些非常简单的东西。我正在尝试在散点图中绘制3D矩阵。这就是我所拥有的

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()

简单,对吧?但我不断收到以下错误:

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'

我不知道如何解决。我哪里错了?代码与this非常相似,所以也许我正在削减矩阵错误或什么? This也没有解决这个问题。有什么想法吗?

编辑:好的,我最后只是通过旧方式重新切片:

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])

这可能不是最好的方式但我生气了。它有效,所以就是这样。

0 个答案:

没有答案