pyplot中开始点和结束点的不同标记颜色



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

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)]

所以我要做的是在相同索引的点之间画线,比如从(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。这可能吗?

技巧是首先绘制直线(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)

最新更新