从2D numpy数组顶点生成matplotlib 3D散点图错误

1 投票
1 回答
1195 浏览
提问于 2025-04-18 06:13

我现在有点困惑,为什么这个不管用。我从一个csv文件里把一堆浮点数据读进了一个numpy数组,然后我只想根据数组中的三列数据来创建一个三维散点图。

#import data from the csv file
data = np.genfromtxt('data.csv', delimiter=',', dtype=float, skiprows=1)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data[:,1], data[:,2], data[:,7], c='r', marker='0')
plt.show()

每次我都会遇到一个断言错误:

/usr/lib/pymodules/python2.7/matplotlib/path.pyc in __init__(self, vertices, codes, _interpolation_steps, closed)
127             codes[-1] = self.CLOSEPOLY
128 
--> 129         assert vertices.ndim == 2
130         assert vertices.shape[1] == 2
131 

AssertionError:

我刚刚搞明白了,但我还是想发这个,因为这是我见过的最没用的错误信息。问题出在这里:

ax.scatter(data[:,1], data[:,2], data[:,7], c='r', marker='0')

marker='0' 是无效的,我本来是想输入 marker='o',修正后就正常工作了。

1 个回答

2

你可以使用Axes3DSubplot对象的scatter3D()方法:

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter3D(data[:,1], data[:,2], data[:,7], c='r', marker='0')

撰写回答