pyplot中起点和终点的不同标记颜色

nop*_*ope 1 python matplotlib

我正在尝试在两个点元组之间的图上绘制线条。我有以下数组:

start_points = [(54.6, 35.2), (55.5, 32.7), (66.5, 23.7), (75.5, 47.8), (89.3, 19.7)]
end_points = [(38.9, 44.3), (46.7, 52.2), (72.0, 1.4), (62.3, 18.9), (80.8, 26.2)]
Run Code Online (Sandbox Code Playgroud)

所以我想做的是在相同索引的点之间绘制线条,例如从 (54.6, 35.2) 到 (38.9, 44.3) 的一条线,从 (55.5, 32.7) 到 (46.7, 52.2) 的另一条线等等。

我通过绘图实现了这一点zip(start_points[:5], end_points[:5]),但我想要不同的标记样式来表示线条的起点和终点。例如,我希望 start_points 为绿色圆圈,end_points 为蓝色 x。这可能吗?

Mar*_*iet 6

技巧是首先绘制直线 ( plt.plot),然后使用散点图 ( plt.scatter) 绘制标记。

import numpy as np
from matplotlib import pyplot as plt

start_points = [(54.6, 35.2), (55.5, 32.7), (66.5, 23.7), (75.5, 47.8), (89.3, 19.7)]
end_points = [(38.9, 44.3), (46.7, 52.2), (72.0, 1.4), (62.3, 18.9), (80.8, 26.2)]

for line in zip(start_points, end_points):
    line = np.array(line)
    plt.plot(line[:, 0], line[:, 1], color='black', zorder=1)
    plt.scatter(line[0, 0], line[0, 1], marker='o', color='green', zorder=2)
    plt.scatter(line[1, 0], line[1, 1], marker='x', color='red', zorder=2)
Run Code Online (Sandbox Code Playgroud)