【问题标题】:python matplotlib: mark dots over Line2Dpython matplotlib:在 Line2D 上标记点
【发布时间】:2017-07-24 09:00:26
【问题描述】:

我想在 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 轴弄乱到英尺到第一个图)。

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

【问题讨论】:

  • 彩色点plt.scatter(X, Y, 'g')
  • 感谢您的回答,这几乎就是我为四处走动所做的工作,此解决方案需要对 Y 轴进行一些丑陋的索引。 (X 值是 x_axis[marker],y 的值是 y_axis[marker]。问题是是否有一个很好的方法来使用 Line2D 对象或类似的东西。

标签: python matplotlib


【解决方案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()

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 2012-01-30
    • 1970-01-01
    • 2013-08-12
    • 2012-01-14
    • 1970-01-01
    • 2021-09-04
    • 2021-10-23
    相关资源
    最近更新 更多