python – 添加图例到散点图(PCA)

我是python的新手,发现了这个优秀的PCA双标图建议(Plot PCA loadings and loading in biplot in sklearn (like R’s autoplot)).现在我尝试为不同的目标添加一个图例.但命令plt.legend()不起作用.

有一个简单的方法吗?
例如,虹膜数据带有来自上面链接的双标图代码.

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.decomposition import PCA
import pandas as pd
from sklearn.preprocessing import StandardScaler

iris = datasets.load_iris()
X = iris.data
y = iris.target
#In general a good idea is to scale the data
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)    

pca = PCA()
x_new = pca.fit_transform(X)

def myplot(score,coeff,labels=None):
    xs = score[:,0]
    ys = score[:,1]
    n = coeff.shape[0]
    scalex = 1.0/(xs.max() - xs.min())
    scaley = 1.0/(ys.max() - ys.min())
    plt.scatter(xs * scalex,ys * scaley, c = y)
    for i in range(n):
        plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
        if labels is None:
            plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color = 'g', ha = 'center', va = 'center')
        else:
            plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()

#Call the function. Use only the 2 PCs.
myplot(x_new[:,0:2],np.transpose(pca.components_[0:2, :]))
plt.show()

欢迎任何关于PCA biplots的建议!
还有其他代码,如果以另一种方式添加图例更容易!

解决方法:

我最近提出了一种向散点图添加图例的简单方法,请参阅GitHub PR.这仍在讨论中.

在此期间,您需要从y中的唯一标签手动创建图例.对于它们中的每一个,您将使用与散点图中使用的标记相同的标记创建Line2D对象,并将它们作为参数提供给plt.legend.

scatter = plt.scatter(xs * scalex,ys * scaley, c = y)
labels = np.unique(y)
handles = [plt.Line2D([],[],marker="o", ls="", 
                      color=scatter.cmap(scatter.norm(yi))) for yi in labels]
plt.legend(handles, labels)

python  – 添加图例到散点图(PCA)

上一篇:《文章翻译》PCA&SVD


下一篇:python – PCA分析后的特征/变量重要性