3d Plot Python 元组索引超出范围

3d Plot Python Tuple Index out of Range

我正在尝试绘制簇质心的 3d 图,但它说元组索引超出范围。你能帮助我吗?这是代码:

from mpl_toolkits.mplot3d import Axes3D

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

ax.set_xlabel("DER (St.)")
ax.set_ylabel("ROA (St.)")
ax.set_zlabel("NPM (St.)")

c0 = (-0.12855869, -0.49862774, -0.18206914)
c1 = (-2.76121712, 2.90472514, -3.18620993)
c2 = (0.78315222,-0.4328264, 0.81931112)
c3 = (-0.38753925,1.35436192,-0.18206914)
c4 = (-1.96191743,0.09194179,-2.18482966)
c5 = (1.81659021,-0.41859101,1.82069139)
clusters = [c0, c1, c2, c3, c4, c5]

# plot 
colors = ['r', 'b', 'y', 'c','m','g']
for i, c in enumerate(clusters):
    ax.scatter(c[0], c[1], c[2], c[3], c[4], c[5], c=colors[i], label='cluster {}'.format(i))

ax.legend(bbox_to_anchor = (1.5, 1))
plt.show()

如果你打印出来'clusters',你会看到每个维度都有三个值。

打印(簇)

[(-0.12855869, -0.49862774, -0.18206914), (-2.76121712, 2.90472514, -3.18620993), (0.78315222, -0.4328264, 0.81931112), (-0.38753925, 1.35436192, -0.18206914), (-1.96191743, 0.09194179, -2.18482966), (1.81659021, -0.41859101, 1.82069139)]

你要求五个c[0], c[1], c[2], c[3], c[4], c[5]

请求三个是正确的c[0], c[1], c[2]

因此(元组索引超出范围)。有了第三维度,你就转向不存在的第四和第五维度。

即在循环中填写数据应该是这样的:

for i, c in enumerate(clusters):
    ax.scatter(c[0], c[1], c[2], c=colors[i], label='cluster {}'.format(i))

如果我的回答对您有帮助,请投票(勾选上下三角下方的方框)。