Python Matplotlib打印空间分隔数据数组

Python Matplotlib打印空间分隔数据数组,python,arrays,numpy,matplotlib,plot,Python,Arrays,Numpy,Matplotlib,Plot,假设每条线都包含一个点的x和y坐标,那么格式化数组数据的最简单方法是什么,这样我就可以迭代连接图上的点 import matplotlib.pyplot as plt (...) <class 'numpy.ndarray'> [-22.58343371 7.97162262] [-49.08400669 -28.64111278] [-71.47754547 -25.78248676] [-46.27120899 -21.72541444] [ 43.6158669 109

假设每条线都包含一个点的x和y坐标,那么格式化数组数据的最简单方法是什么,这样我就可以迭代连接图上的点

import matplotlib.pyplot as plt
(...)

<class 'numpy.ndarray'>
[-22.58343371   7.97162262]
[-49.08400669 -28.64111278]
[-71.47754547 -25.78248676]
[-46.27120899 -21.72541444]
[ 43.6158669  109.61815799]
[-22.58343371   7.97162262]

(...)

plt.plot(x, y, color='orange')

您可能需要对数据进行排序,以获得所需的可视化效果:

将numpy导入为np
将matplotlib.pyplot作为plt导入
a=np.数组([-22.58343371,7.97162262],
[-49.08400669, -28.64111278],
[-71.47754547, -25.78248676],
[-46.27120899, -21.72541444],
[ 43.6158669 , 109.61815799],
[-22.58343371,   7.97162262]])
order=a[:,0].argsort()
plt.plt(a[order,0],a[order,1],color='orange',marker='o',linestyle='-')
plt.show()

如果不对其进行排序,您将得到如下结果:

将numpy导入为np
将matplotlib.pyplot作为plt导入
a=np.数组([-22.58343371,7.97162262],
[-49.08400669, -28.64111278],
[-71.47754547, -25.78248676],
[-46.27120899, -21.72541444],
[ 43.6158669 , 109.61815799],
[-22.58343371,   7.97162262]])
plt.plt(a[:,0],a[:,1],颜色为橙色,标记为o,线型为“-”)
plt.show()

有时候,在许多小时之后,事情会变得比实际情况更困难:)谢谢,太多了,卡波西!工作方案如下:

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np

n = 5
order = [0, 2, 1, 3, 4, 0]

distances = [[0, 39, 22, 59, 54, 33, 57, 32, 89, 73, 29, 46],
             [39, 0, 20, 20, 81, 8, 49, 64, 63, 84, 10, 61],
             [22, 20, 0, 39, 74, 18, 60, 44, 71, 73, 11, 46],
             [59, 20, 39, 0, 93, 27, 51, 81, 48, 80, 30, 69],
             [54, 81, 74, 93, 0, 73, 43, 56, 104, 76, 76, 77],
             [33, 8, 18, 27, 73, 0, 45, 61, 71, 88, 8, 63]]

pca = PCA(n_components=2)
coord = pca.fit_transform(distances[:n])

plt.scatter(coord[:,0], coord[:,1])

for i in coord:
    x = np.where(coord == i)
    plt.annotate((x[0][0]) ,i, color='red')

x = coord[order]
print(x)

plt.plot(x[:, 0], x[:, 1], color='orange', marker='o', linestyle='-')
plt.show()

请张贴完整的代码。另外,你所说的“在图上迭代连接点”是什么意思?
plt.plot(arr[:,0],arr[:,1])
?谢谢你,卡波西,但我想按给定的顺序绘制线。
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np

n = 5
order = [0, 2, 1, 3, 4, 0]

distances = [[0, 39, 22, 59, 54, 33, 57, 32, 89, 73, 29, 46],
             [39, 0, 20, 20, 81, 8, 49, 64, 63, 84, 10, 61],
             [22, 20, 0, 39, 74, 18, 60, 44, 71, 73, 11, 46],
             [59, 20, 39, 0, 93, 27, 51, 81, 48, 80, 30, 69],
             [54, 81, 74, 93, 0, 73, 43, 56, 104, 76, 76, 77],
             [33, 8, 18, 27, 73, 0, 45, 61, 71, 88, 8, 63]]

pca = PCA(n_components=2)
coord = pca.fit_transform(distances[:n])

plt.scatter(coord[:,0], coord[:,1])

for i in coord:
    x = np.where(coord == i)
    plt.annotate((x[0][0]) ,i, color='red')

x = coord[order]
print(x)

plt.plot(x[:, 0], x[:, 1], color='orange', marker='o', linestyle='-')
plt.show()