Matplotlib 多散点图选择器
Matplotlib multiple scatter plot picker
我正在尝试按照 this demo 处理散点图上的拾取事件。该演示适用于单个散点图。但是,当我添加第二个散点图时,我只会在单击这些点时获得其中一个图的索引。这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand
def pick_scatter_plot():
# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
x, y, c, s = rand(4, 2)
def onpick3(event):
ind = event.ind
print('onpick3 scatter:', ind, x[ind], y[ind])
fig, ax = plt.subplots()
#first scatter plot:
ax.scatter(x, y, 100, c, marker='o', picker=True)
#second scatted plot
ax.scatter(x*2, y, 50, c, marker='s', picker=True)
fig.canvas.mpl_connect('pick_event', onpick3)
if __name__ == '__main__':
pick_scatter_plot()
plt.show()
第一组用标记 'o' 绘制,第二组用标记 's' 绘制,如下所示:
然而,当我点击所有四个点时,我只得到索引 0 和 1。这是当我一一点击所有 4 个点时的输出:
('onpick3 scatter:', array([1], dtype=int32), array([0.18243891]), array([0.30505569]))
('onpick3 scatter:', array([1], dtype=int32), array([0.18243891]), array([0.30505569]))
('onpick3 scatter:', array([0], dtype=int32), array([0.35977978]), array([0.66748979]))
('onpick3 scatter:', array([0], dtype=int32), array([0.35977978]), array([0.66748979]))
我的问题是如何正确访问两个散点图的点的索引?将所有数据组合成一个散点图对我来说不起作用,因为我有多个数据集,我需要用不同的标记进行绘图,并且 scatter
不允许使用标记列表。
据我了解,您必须首先确定您点击的是哪个散点,然后使用它从正确的数组中获取点。我通过标签识别每个散点,然后,如果它是 _collection0
从数组 x
、y
获取点,如果它来自 _collection1
从 x2, y2
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand
def pick_scatter_plot():
# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
x, y, c, s = rand(4, 2)
def onpick3(event):
ind = event.ind
label = event.artist.get_label()
if label == '_collection0':
print('onpick3 scatter:', label, ind, x[ind], y[ind])
elif label == '_collection1':
print('onpick3 scatter:', label, ind, (x*2)[ind], (y*2)[ind])
fig, ax = plt.subplots()
#first scatter plot:
ax.scatter(x, y, 100, c, marker='o', picker=True)
#second scatted plot
ax.scatter(x*2, y, 50, c, marker='s', picker=True)
fig.canvas.mpl_connect('pick_event', onpick3)
if __name__ == '__main__':
pick_scatter_plot()
plt.show()
这是点击所有四个点后的输出:
onpick3 scatter: _collection0 [0] [0.56367835] [0.45595969]
onpick3 scatter: _collection0 [1] [0.71088259] [0.22692447]
onpick3 scatter: _collection1 [0] [1.12735669] [0.91191939]
onpick3 scatter: _collection1 [1] [1.42176517] [0.45384893]
您可以看到所有的组合:
集合0:点0,点1;
集合1:第0点,第1点
我正在尝试按照 this demo 处理散点图上的拾取事件。该演示适用于单个散点图。但是,当我添加第二个散点图时,我只会在单击这些点时获得其中一个图的索引。这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand
def pick_scatter_plot():
# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
x, y, c, s = rand(4, 2)
def onpick3(event):
ind = event.ind
print('onpick3 scatter:', ind, x[ind], y[ind])
fig, ax = plt.subplots()
#first scatter plot:
ax.scatter(x, y, 100, c, marker='o', picker=True)
#second scatted plot
ax.scatter(x*2, y, 50, c, marker='s', picker=True)
fig.canvas.mpl_connect('pick_event', onpick3)
if __name__ == '__main__':
pick_scatter_plot()
plt.show()
第一组用标记 'o' 绘制,第二组用标记 's' 绘制,如下所示:
然而,当我点击所有四个点时,我只得到索引 0 和 1。这是当我一一点击所有 4 个点时的输出:
('onpick3 scatter:', array([1], dtype=int32), array([0.18243891]), array([0.30505569]))
('onpick3 scatter:', array([1], dtype=int32), array([0.18243891]), array([0.30505569]))
('onpick3 scatter:', array([0], dtype=int32), array([0.35977978]), array([0.66748979]))
('onpick3 scatter:', array([0], dtype=int32), array([0.35977978]), array([0.66748979]))
我的问题是如何正确访问两个散点图的点的索引?将所有数据组合成一个散点图对我来说不起作用,因为我有多个数据集,我需要用不同的标记进行绘图,并且 scatter
不允许使用标记列表。
据我了解,您必须首先确定您点击的是哪个散点,然后使用它从正确的数组中获取点。我通过标签识别每个散点,然后,如果它是 _collection0
从数组 x
、y
获取点,如果它来自 _collection1
从 x2, y2
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand
def pick_scatter_plot():
# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
x, y, c, s = rand(4, 2)
def onpick3(event):
ind = event.ind
label = event.artist.get_label()
if label == '_collection0':
print('onpick3 scatter:', label, ind, x[ind], y[ind])
elif label == '_collection1':
print('onpick3 scatter:', label, ind, (x*2)[ind], (y*2)[ind])
fig, ax = plt.subplots()
#first scatter plot:
ax.scatter(x, y, 100, c, marker='o', picker=True)
#second scatted plot
ax.scatter(x*2, y, 50, c, marker='s', picker=True)
fig.canvas.mpl_connect('pick_event', onpick3)
if __name__ == '__main__':
pick_scatter_plot()
plt.show()
这是点击所有四个点后的输出:
onpick3 scatter: _collection0 [0] [0.56367835] [0.45595969]
onpick3 scatter: _collection0 [1] [0.71088259] [0.22692447]
onpick3 scatter: _collection1 [0] [1.12735669] [0.91191939]
onpick3 scatter: _collection1 [1] [1.42176517] [0.45384893]
您可以看到所有的组合: 集合0:点0,点1; 集合1:第0点,第1点