【问题标题】:matplotlib.pyplot.legend() returns emptymatplotlib.pyplot.legend() 返回空
【发布时间】:2021-08-23 14:12:43
【问题描述】:

您好,我正在研究 winkosisn 乳腺癌数据集。我已经对其应用了 PCA 并将结果存储在 df1 中。 df2 包含应用 KMeans 时获得的类类型的标签。然后我将 df1 和 df2 连接起来。

为了可视化结果,我使用了 matplotlib。绘制了数据点和聚类中心,但未显示图例。它只是返回空。

谁能说出为什么会这样?

import pandas as pd
df1 = pd.DataFrame(x_trans,columns=['Feature1','Feature2'])
df2 = pd.DataFrame(data = km.labels_,columns=['Breast_Cancer_Type'])
df3 = pd.concat([df1,df2],axis=1)
print(df3)

import matplotlib.pyplot as plt

plt.scatter(df3.iloc[:,0],df3.iloc[:,1],c=km.labels_,cmap='rainbow')
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color='black')
plt.legend()
plt.show()

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:

    您必须在plt.scatter() 函数中添加标签。看到这些examples

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 2021-07-07
      • 2013-09-30
      • 2013-04-07
      • 2018-05-21
      • 2016-01-01
      • 2020-11-05
      • 1970-01-01
      • 2011-09-23
      相关资源
      最近更新 更多