你点击了 LineCollection 中的哪一行,如何知道?

huangapple go评论65阅读模式
英文:

How Do I know on which line I clicked on in a LineCollection?

问题

以下是您要的中文翻译部分:

我想知道在从LineCollection中的pick_event中点击的哪一行。
通常我逐行绘制信号,可以通过event.artist._label访问每一行的信息,但在LineCollection的情况下,情况并不那么简单。
如果我点击第二段,我想找到一个包含1的变量,例如。
有办法做到吗?

这是一个最小的示例:

from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand

def onpick1(event):
    print('event')
    if isinstance(event.artist, LineCollection):
        print('LineCollection')
        thoselines = event.artist
        print('我点击了哪条线?')

def linecoll(N):
    x = np.random.rand(N, 3)
    y = np.random.rand(N, 3)
    C = np.random.rand(N, 3)
    L = [str(i) for i in range(N)]
    data = np.stack((x, y), axis=2)
    fig, ax = plt.subplots()
    fig.canvas.mpl_connect('pick_event', onpick1)
    lines = ax.add_collection(LineCollection(data, colors=C))
    lines.set_picker(2)
    lines.labels = L
    plt.show()

linecoll(10)

如果您有任何其他翻译需求,请告诉我。

英文:

I would like to know on which line I clicked on in a pick_event from a LineCollection.
Usually I plot signals line by line and I can access to each line information through event.artist._label but in the case of LineCollection, this is not that simple.
If I click on the second segment, I would like to find a variable that contains 1 for instance.
Is there a way to do that?

Here's a minimal example:

from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand

def onpick1(event):
    print('event')
    if isinstance(event.artist, LineCollection):
        print('LineCollection')
        thoselines = event.artist
        print('Which line did I clicked on ? ')

def linecoll(N):
    x = np.random.rand(N, 3)
    y = np.random.rand(N, 3)
    C = np.random.rand(N, 3)
    L = [str(i) for i in range(N)]
    data = np.stack((x, y), axis=2)
    fig, ax = plt.subplots()
    fig.canvas.mpl_connect('pick_event', onpick1)
    lines = ax.add_collection(LineCollection(data,colors=C ))
    lines.set_picker(2)
    lines.labels = L
    plt.show()

linecoll(10)

答案1

得分: 1

根据文档,您可以使用 event.ind

from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand

def onpick1(event):
    print('事件')
    if isinstance(event.artist, LineCollection):
        print('线集合')
        thoselines = event.artist
        print('我点击了哪条线?')
        print(event.ind)  # <- 在这里

def linecoll(N):
    x = np.random.rand(N, 3)
    y = np.random.rand(N, 3)
    C = np.random.rand(N, 3)
    L = [str(i) for i in range(N)]
    data = np.stack((x, y), axis=2)
    fig, ax = plt.subplots()
    fig.canvas.mpl_connect('pick_event', onpick1)
    lines = ax.add_collection(LineCollection(data,colors=C ))
    lines.set_picker(2)
    lines.labels = L
    plt.show()

linecoll(10)

输出:

事件
线集合
我点击了哪条线
[6]
事件
线集合
我点击了哪条线
[5]
事件
线集合
我点击了哪条线
[3 6]  # <- 交点

请注意,如果您点击两条线的交点,event.ind 将包含两个LineCollection的索引。

英文:

According the documentation, you can use event.ind:

from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand

def onpick1(event):
    print(&#39;event&#39;)
    if isinstance(event.artist, LineCollection):
        print(&#39;LineCollection&#39;)
        thoselines = event.artist
        print(&#39;Which line did I clicked on ? &#39;)
        print(event.ind)  # &lt;- HERE

def linecoll(N):
    x = np.random.rand(N, 3)
    y = np.random.rand(N, 3)
    C = np.random.rand(N, 3)
    L = [str(i) for i in range(N)]
    data = np.stack((x, y), axis=2)
    fig, ax = plt.subplots()
    fig.canvas.mpl_connect(&#39;pick_event&#39;, onpick1)
    lines = ax.add_collection(LineCollection(data,colors=C ))
    lines.set_picker(2)
    lines.labels = L
    plt.show()

linecoll(10)

Output:

event
LineCollection
Which line did I clicked on ? 
[6]
event
LineCollection
Which line did I clicked on ? 
[5]
event
LineCollection
Which line did I clicked on ? 
[3 6]  # &lt;- intersection

Note, if you click on an intersection of 2 lines, event.ind will contain the index of both LineCollection.

huangapple
  • 本文由 发表于 2023年6月1日 14:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定