Python 与散点图点相关的附加信息问题

Python 与散点图点相关的附加信息问题,python,matplotlib,plot,Python,Matplotlib,Plot,我正在尝试使用PCA降低数据集的维数。然后,我根据一些标准(取决于数据点所来自的文件名的数量)为每个数据点分配一个“类/类别”,并将所有内容绘制为带有遗留文件的散点图 在每个数据点的另一个列表中,我存储了一些附加信息,我希望每个数据点都是可拾取的,这样我就可以在终端中读取关于它的信息。 在绘制散点图时(我假设是因为我按子集绘制),顺序会变得混乱。 接收到的事件的标记不再适用于具有附加信息的阵列 我试图在绘图时对信息数组重新排序,但不知何故,它仍然不起作用。这是我的密码: targets = []

我正在尝试使用PCA降低数据集的维数。然后,我根据一些标准(取决于数据点所来自的文件名的数量)为每个数据点分配一个“类/类别”,并将所有内容绘制为带有遗留文件的散点图

在每个数据点的另一个列表中,我存储了一些附加信息,我希望每个数据点都是可拾取的,这样我就可以在终端中读取关于它的信息。 在绘制散点图时(我假设是因为我按子集绘制),顺序会变得混乱。 接收到的事件的标记不再适用于具有附加信息的阵列

我试图在绘图时对信息数组重新排序,但不知何故,它仍然不起作用。这是我的密码:

targets = []
trainNames = []

# React on to a click on a datapoint.
def onPick(event):
  indexes = event.ind
  xy = event.artist.get_offsets()
  for index in indexes:
    print trainNames[index]


# Load the additonal information for each datapoint. It's stored in the
# same order as the datapoints in 'trainingfile.csv'.
modelNamesFile = open("training-names.csv") 
for line in modelNamesFile:

  # Save target for datapoint. It's the class of the object, seperated
  # into "rectangular", "cylindrical", "irregular", dependend on the
  # objects file number.
  objnum = int(line.split(",")[-1].split("/")[-1].split(".")[0])
  if (objnum <= 16):
    objnum = 0
  elif (objnum >= 17 and objnum <= 34):
    objnum = 1
  else:
    objnum = 2
  targets.append(objnum)

  # Save name description for datapoint.
  sceneName = line.split(",")[0].split("/")[-1]
  modelName = line.split(",")[-1].split("/")[-1].split(".")[0]
  trainNames.append(sceneName + ", " + modelName)


target_names = ["rectangular", "cylindrical", "irregular"]


# Load the actual data.
f = open("trainingfile.csv")
tData = []
for line in f:
  lsplit = line.split(",")
  datapoint = []
  for feature in lsplit:
    datapoint.append(float(feature))

  tData.append(datapoint)
data = np.array(tData) 

# Transform it into 2D with PCA.
y = np.array(targets)
X = np.delete(data, data.shape[1] - 1, 1) # Strip class.
pipeline = Pipeline([('scaling', StandardScaler()), ('pca', PCA(n_components=2))])
X_reduced = pipeline.fit_transform(data)


# Create plot.
trainNames = np.array(trainNames)
tmpTrainNames = np.array([])
fig = plt.figure()
for c, i, target_name in zip("rgb", [0, 1, 2], target_names):
  plt.scatter(X_reduced[y == i, 0], X_reduced[y == i, 1], c=c, label=target_name, picker=True)

  # Here i try to rearrange the order of the additonal information int he order the points
  # were plotted.
  tmpTrainNames = np.append(tmpTrainNames, trainNames[y == i])

trainNames = tmpTrainNames

plt.legend()
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
fig.canvas.mpl_connect('pick_event', onPick)
plt.show()
targets=[]
列车名称=[]
#对点击数据点做出反应。
def onPick(事件):
索引=event.ind
xy=event.artist.get_offset()
对于索引中的索引:
打印列车名称[索引]
#加载每个数据点的附加信息。它储存在仓库里
#与“trainingfile.csv”中的数据点顺序相同。
modelNamesFile=open(“training names.csv”)
对于modelNamesFile中的行:
#保存数据点的目标。它是对象的类,分开的
#分为“长方形”、“圆柱形”、“不规则形”,依形状而定
#对象文件号。
objnum=int(行.split(“,”[-1]。split(“/”[-1]。split(“.”[0]))

如果(objnum=17和objnum,因为我找不到索引问题的解决方案,所以我用一种不同的、更简单的方法解决了它
,我直接将颜色值指定为类,并将整个类目标作为颜色参数。这样我可以一次绘制所有内容,并保持数据点的原始顺序

# y is here the class target with color values, e.g. ['r', 'g',..., 'r']
plt.scatter(X_reduced[:,0], X_reduced[:,1], c=y, picker=True)

因为我找不到索引问题的解决方案,所以我用一种不同的、有点粗糙的方法解决了它,我直接将颜色值指定为类,并将整个类目标作为颜色参数。这样我可以一次绘制所有内容,并保持数据点的原始顺序

# y is here the class target with color values, e.g. ['r', 'g',..., 'r']
plt.scatter(X_reduced[:,0], X_reduced[:,1], c=y, picker=True)