[FIXED] 3d Plot Python Tuple Index out of Range

Issue

i'm trying to 3d plot of cluster centroids, but it said tuple index out of range. Can you help me? Here are the code:

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

Solution

If you print out 'clusters', you will see that there are three values in each dimension.

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

You request five c[0], c[1], c[2], c[3], c[4], c[5]

It would be correct to request three c[0], c[1], c[2]

Therefore(Tuple Index out of Range). Having dimension three, you turn to the fourth and fifth dimension, which do not exist.

That is, filling in the data in the loop should look like this:

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

If my answer helped you, vote (check the box below the upper and lower triangle). enter image description here



Answered By - inquirer
Answer Checked By - Mildred Charles (Python.Easybugfix Admin)

Related Posts

Post a Comment