使用matplotlib连接两个散在的点

时间:2018-02-09 07:47:43

标签: python matplotlib

我正在尝试使用matplotlib连接两个点。 例如,

A=[[1,2],[3,4],[5,6]]
B=[8,1]

我应该将每个(1,2),(3,4),(5,6)三个点连接到(8,1), 我尝试使用类似的方法(不是这个,但与此类似)

xs = [x[0] for x in A]
ys = [y[1] for y in A]
plt.plot(xs,ys)

但是这样,我应该在每三个点之间复制(8,1)。

是否有任何pythonic方法可以做到这一点?

2 个答案:

答案 0 :(得分:4)

如果您想要连接到每个A点的B点多于一个,则可以使用itertools方法。当然,每个列表也只有一个点。

from matplotlib import pyplot as plt 
from itertools import product

A = [[1,2], [3,4], [5,6]]
B = [[8,1], [5,7], [3,1]]

for points in product(A, B):
    point1, point2 = zip(*points)
    plt.plot(point1, point2)

plt.show()

答案 1 :(得分:3)

实际上,您要做的是将多个点连接到一个点,为每个连接创建一条线 - 多对一映射

考虑到这一点,这是完全可以接受的:

/staging/dusk/inbound/ --> Main Directory

Dir1
File1.txt,File2.txt,..sample1.doc,sample2.pdf,File*.txt  --> we have to Merge the File name which starts with Fil*.txt -->Final.txt

Dir2
File1.txt,File2.txt,..attach1.txt,sample1.doc, File*.txt --> we have to Merge the File name which starts with Fil*.txt -->Final.txt

Dir3
File1.txt,File2.txt,File*.txt,..sample1,sample2*.txt --> we have to Merge the File name which starts with Fil*.txt -->Final.txt

Dir4
File1.txt,File2.txt,File*.txt,..temp.doc,attach.txt --> we have to Merge the File name which starts with Fil*.txt -->Final.txt

Di5
File1.txt,File2.txt,File*.txt,..sample1,sample2*.txt --> we have to Merge the File name which starts with Fil*.txt -->Final.txt

Dir"n"
File1.txt,File2.txt,File3.txt,File*.txt..attach1,attach*.txt --> we have to Merge the File name which starts with Fil*.txt -->Final.txt

结果为:enter image description here