Skip to main content
 首页 » 编程设计

python之使用 matplotlib 连接两个分散的点

2025年05月04日12hnrainll

我正在尝试使用 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 方法可以做到这一点?

请您参考如下方法:

如果您有多个点 B 并希望连接到每个点 A,则可以使用 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()