python matplotlib : mark dots over Line2D

标签 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 轴弄乱到第一个图的英尺)。

无论如何,很确定有一个正确的方法可以做到这一点, 有任何想法吗?

最佳答案

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

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

关于python matplotlib : mark dots over Line2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45269826/

相关文章:

Python EXE 文件创建

python - 使用相同的符号生成许多 SymPy 表达式

python - 根据字典的键打破字典列表而不丢失顺序

python - 阻止 matplotlib 绘图关闭

python - python 中 matlibplot 中散点图的缩放轴

python-2.7 - 带有 pyplot 的直方图中的垂直线

python - Matplotlib 在给定 csv 数据的情况下创建曲面图 (x,y,z,color) - 颜色错误

python 创建一个新的 csv 并从列表中填充标题行

python - Seaborn PairGrid : show axes labels for each subplot

python - Seaborn X 轴分类数据