Python 向散点图(PCA)添加图例

Python 向散点图(PCA)添加图例,python,matplotlib,legend,pca,biplot,Python,Matplotlib,Legend,Pca,Biplot,我是python的新手,发现了这个非常好的PCA双标图建议()。现在,我尝试为不同的目标添加图例。但是命令plt.legend()不起作用 有没有简单的方法? 作为一个例子,iris数据与上面链接中的双批次代码一致 import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.decomposition import PCA import pandas as pd from

我是python的新手,发现了这个非常好的PCA双标图建议()。现在,我尝试为不同的目标添加图例。但是命令
plt.legend()
不起作用

有没有简单的方法? 作为一个例子,iris数据与上面链接中的双批次代码一致

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双批次提出任何建议!
还有其他代码,如果以另一种方式添加图例更容易

我最近提出了一种简单的方法,可以在散点中添加图例,请参见。这仍在讨论中

同时,您需要从
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)

尝试“pca”库。这将绘制解释的方差,并创建一个双图

pip install pca

from pca import pca

# Initialize to reduce the data up to the number of componentes that explains 95% of the variance.
model = pca(n_components=0.95)

# Or reduce the data towards 2 PCs
model = pca(n_components=2)

# Load example dataset
import pandas as pd
import sklearn
from sklearn.datasets import load_iris
X = pd.DataFrame(data=load_iris().data, columns=load_iris().feature_names, index=load_iris().target)

# Fit transform
results = model.fit_transform(X)

# Plot explained variance
fig, ax = model.plot()


结果是一个dict,其中包含许多关于PC、装载量等的统计数据。您阅读了吗?是的,但我不明白如何将其添加到现有代码中:(
# Scatter first 2 PCs
fig, ax = model.scatter()

# Make biplot with the number of features
fig, ax = model.biplot(n_feat=4)