如何在python中使用Axes3D中的循环绘制图例?

如何在python中使用Axes3D中的循环绘制图例?,python,matplotlib,plot,Python,Matplotlib,Plot,我有以下Python数组: numbers=[75, 100, 680, 123, 4, 4, 8, 15] 这些数字对应于与特定簇关联的点数 因此,在绘制簇点的同时,我希望有一个图例,它提供了以下信息: "Cluster 1 has 75 points" "Cluster 2 has 100 points" 等等 我很难对循环进行编码,因此非常感谢您的帮助。更新: 在提供更多信息后,我认为您需要: import numpy as np import matplotlib.pyplot a

我有以下Python数组:

numbers=[75, 100, 680, 123, 4, 4, 8, 15]
这些数字对应于与特定簇关联的点数

因此,在绘制簇点的同时,我希望有一个图例,它提供了以下信息:

"Cluster 1 has 75 points"

"Cluster 2 has 100 points"
等等

我很难对循环进行编码,因此非常感谢您的帮助。

更新: 在提供更多信息后,我认为您需要:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# random data: 800 samples, 8 coordinates
X_scaled = np.random.rand(800,8)

# some labels e.g. output of clustering algorithm
labels = np.concatenate([np.ones(400),np.zeros(400)])

# unique classes/groups in the data
number_of_classes = np.unique(labels).shape[0]

# the desired legends
legends = ['cluster 1', 'cluster 2']

# colors for the groups
colors = ["r","b"]


fig1 = plt.figure() 
ax = Axes3D(fig1) 

for i in range(number_of_classes):
    ax.scatter(X_scaled[:,0][labels==i], X_scaled[:,1][labels==i],X_scaled[:,7][labels==i], c = colors[i] ,s=50, label= legends[i] + " has {} points".format(X_scaled[:,0][labels==i].shape[0]))

plt.legend()
plt.savefig("test.png", dpi = 300)
plt.show()

编辑1:如何使用for循环创建图例

legends = []
for i in range(5):
    legends.append('cluster{}'.format(i))

print(legends)
['cluster0', 'cluster1', 'cluster2', 'cluster3', 'cluster4']

你想做什么样的阴谋?嗨,很抱歉反应太晚了。我想要一个点的3D图。现在我使用下面的代码:fig1=plt.figure()ax=Axes3D(fig1)ax。X_scaled中的散点(X_scaled[:,0],X_scaled[:,1],X_scaled[:,7],c=labels,cmap='Set1',s=50)保存簇点的坐标。np.shape(X_scaled)=(800,8)添加完整的代码和数据实际上还有其他内容。只需读取数据并应用DBSCAN即可。现在是否更清楚问题是什么?min_max_scaler=预处理。min_max_scaler(特征范围=(0,1))X_scaled=min_max_scaler。fit_transform(数据)eps=0.28 min_numbers=3 dbscan=dbscan(eps=eps,min_samples=min_numbers)。fit(X_scaled)labels=dbscan.labels G1=plt.figure()ax=Axes3D(fig1)ax.scatter(X_scaled[:,0],X_scaled[:,1],X_scaled[:,7],c=labels,cmap='Set1',s=50)请看我的最新答案。这对我来说很好,谢谢。我还有最后一个问题。如何创建带有循环的数组“图例”?我试过了,但没用。。。。。范围(5)中的i的图例=[]:项=['群集%d'%i]。附加([i])打印(图例)请参见我的答案中的“编辑1”