python matplotlib:在Line2D上标记点

时间:2017-07-23 20:54:33

标签: python matplotlib

我想在Line2D图上标记一些点。 我的标记是一个指标列表。 (标记的格式可以不同,但​​我必须有多个标记)。

例如:

import matplotlib.pyplot as plt
x_axis  = [...]
y_axis  = [1,2,3,4,5]
plt.plot(x_axis,y_axis)
marker1 = [0,0,1,0,0]
marker2 = [1,0,0,0,0]
# TODO: mark with green dots where marker1 == 1,
#       mark with red dots where marker2 ==1.
我解决这个问题的方法是制作另一个情节 (只是将x和y轴弄乱到第一个图的脚)。

无论如何,非常确定有正确的方法, 有任何想法吗?

1 个答案:

答案 0 :(得分:1)

您可以为每种颜色绘制一个图形,通过标记过滤原始数组,这非常直接实现:

import numpy as np
import matplotlib.pyplot as plt

x_axis  = np.array([1,2,3,4,5])
y_axis  = np.array([1,2,3,4,5])
plt.plot(x_axis,y_axis)
marker1 = np.array([0,0,1,0,0]).astype(bool)
marker2 = np.array([1,0,0,0,0]).astype(bool)

#       mark with green dots where marker1 == 1,
#       mark with red dots where marker2 ==1.
for m, c in zip([marker1, marker2],["g","r"]):
    plt.plot(x_axis[m], y_axis[m], color=c, ls="", marker="o")

plt.show()

enter image description here