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

标签 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 方法可以做到这一点?

最佳答案

如果您有多个点 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()

关于python - 使用 matplotlib 连接两个分散的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701013/

相关文章:

python - 使用 Databricks 中的 PySpark 在 Azure DataLake 中使用 partitionBy 和覆盖策略

python - 检查python中的sql表是否存在

loops - 如何使Matplotlib保存gif循环

wxpython - 修复 Windows 上的全黑 wx 光标

python - 设置xlim后如何根据屏幕上显示的数据自动设置ylim

python - matplotlib savefig 修剪图形

python - Stop Piston 的错误捕获

python - 使用python PIL读取BMP RGBA不起作用

python - 如何为每个销售订单检索 many2many 字段的值?

matplotlib - Julia PyPlot : plot 3D surface with as face colors the norm of surface gradient