Matplotlib 没有显示我的散点图?

Matplotlib is not showing my scatterplot?

当我使用 plt.show 时,该图仅显示 PCA 线,而不是前 2 个虹膜特征的散点图

import numpy as np
import matplotlib.pylab as plt
from sklearn import decomposition

x = np.load("iris_features.npy")[:, :2]
y = np.load("iris_labels.npy")
idx = np.where(y != 0)
x = x[idx]
x[:, 0] -= x[:, 0].mean()
x[:, 1] -= x[:, 1].mean()


pca = decomposition.PCA(n_components=2)
pca.fit(x)
v = pca.explained_variance_ratio_


plt.scatter(x[:, 0], x[:, 1], marker='o', color='b')
ax = plt.axes()
x0 = v[0] * pca.components_[0, 0]
y0 = v[0] * pca.components_[0, 1]
ax.arrow(0, 0, x0, y0, head_width=0.05,) head_length=0.1, fc='r', ec='r')
x1 = v[1] * pca.components_[1, 0]
y1 = v[1] * pca.components_[1, 1]
ax.arrow(0, 0, x1, y1, head_width=0.05, head_length=0.1, fc='r', ec='r')
plt.xlabel("$x_0$", fontsize=16)
plt.ylabel("$x_1$", fontsize=16)
plt.show()

resulting plot

What the correct plot should look like

您的代码似乎可以使用 sklearn 的鸢尾花数据集正常工作,并产生预期的结果。您可以尝试这样做,或者您可以以可测试的方式共享您的数据集,因为原则上您可以拥有这些 csv 文件中的任何数据。

import numpy as np
import matplotlib.pylab as plt
from sklearn import decomposition
from sklearn import datasets

iris = datasets.load_iris()

x = iris.data
y = iris.target

idx = np.where(y != 0)
x = x[idx]
....  # your code continues here

如果这不起作用,请分享或直接更新您的 python、matplotlib 版本。