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

时间:2016-10-14 07:10:49

标签: 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)]

所以我要做的是在相同指数的点之间绘制线条,如(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。这可能吗?

1 个答案:

答案 0 :(得分:2)

诀窍是首先绘制线条(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)