我是 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 =