英文:
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('event')
if isinstance(event.artist, LineCollection):
print('LineCollection')
thoselines = event.artist
print('Which line did I clicked on ? ')
print(event.ind) # <- 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('pick_event', 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] # <- intersection
Note, if you click on an intersection of 2 lines, event.ind
will contain the index of both LineCollection
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论