如何选择一组线而不是单线?

How to pick a set of lines instead of just a single line?

我正在使用 matplolib 的 Line2D 绘制多条线,并希望一次选择或select所有线。

MWE 提供了一个三角形(可以是任何多边形或形状)并包含一个功能,可以单独选择每条线。我想通过单击来选择整个三角形。另外我注意到如果我添加另一行 onPick 函数根本不起作用。有谁知道我做错了什么吗?

编辑 正如下面评论中所建议的,我添加了一个多边形和一个修改后的函数 pick_simple()(来自:https://matplotlib.org/3.1.0/gallery/event_handling/pick_event_demo.html)。但不幸的是,这带来了新的问题。通过绘制多边形,我得到了一个填充的蓝色补丁,即使我设置了 fill=False 以及线宽和颜色。 pick_simple() 函数也没有做任何让我很困惑的事情。

from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection


fig = plt.figure()
ax = fig.add_subplot(111)

triangle = [[0.1, 0.3],
            [0.2, 0.8],
            [0.3, 0.5],
            [0.1, 0.3]]

for i in range(len(triangle)-1):
    tri = Line2D([triangle[i][0],triangle[i+1][0]],[triangle[i][1],
            triangle[i+1][1]], linewidth=0.75, color='#F97306')
    tri.set_picker(True)
    ax.add_line(tri)

geometry = [[0.0,0.0],[0.1,0.05],[0.2,0.15],[0.3,0.20],[0.4,0.25],[0.5,0.30],
        [0.6,0.25],[0.7,0.15],[0.8,0.05],[0.9,0.025],[1.0,0.0]]

patches = []
polygon = Polygon(geometry, closed=False, fill=False, linewidth=0.75, color='#F97306')
polygon.set_picker(True)
patches.append(polygon)
p = PatchCollection(patches)
ax.add_collection(p)


plt.show()

def pick_simple():
    def onpick(event):
        if isinstance(event.artist, Polygon):
            patch = event.artist
            print('onpick patch:', patch.get_path())
    fig.canvas.mpl_connect('pick_event', onpick)

def pick_factory(ax):
    def onPick(event):
        if event.inaxes == ax:
            for line in ax.lines:
                if line.get_picker():
                    cont, ind = line.contains(event)
                    if cont:
                        line.set_color('#029386')
                        line.set_linewidth(5)
                        ax.figure.canvas.draw_idle()

    fig = ax.get_figure() # get the figure of interest
    fig.canvas.mpl_connect('button_press_event', onPick)

pick_factory(ax)
pick_simple()

有几个小错误。

  • 要选择 collection 的成员,您需要将选择器设置为 collection,而不是初始艺术家。因此,选择回调需要通过 event.ind.
  • 选择被选择的 collection 成员
  • A PatchCollection 的目的通常是设置其 children 本身的属性,如线宽、颜色等。如果你不想这样,你需要使用 match_original=True

完整代码:

from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection


fig = plt.figure()
ax = fig.add_subplot(111)


geometry = [[0.0,0.0],[0.1,0.05],[0.2,0.15],[0.3,0.20],[0.4,0.25],[0.5,0.30],
        [0.6,0.25],[0.7,0.15],[0.8,0.05],[0.9,0.025],[1.0,0.0]]

patches = []
polygon = Polygon(geometry, closed=False, fill=False, linewidth=3, color='#F97306')
patches.append(polygon)
p = PatchCollection(patches, match_original=True)
p.set_picker(True)
ax.add_collection(p)


def pick_simple():
    def onpick(event):
        if isinstance(event.artist, PatchCollection):
            collection = event.artist
            print('onpick collection:', collection)
            print('picked index', event.ind)
            print('path at index', collection.get_paths()[event.ind[0]])
    return fig.canvas.mpl_connect('pick_event', onpick)


cid = pick_simple()
plt.show()