gpt4 book ai didi

python - 正确使用 Matplotlib 的 pick 事件

转载 作者:行者123 更新时间:2023-11-28 21:46:45 26 4
gpt4 key购买 nike

我相信我没有正确使用 Matplotlib 的选择事件。在下面的代码中,我创建了三个不相交的橙色圆盘,它们具有指定的半径和由 ID 号标识的位置。

我想单击-单击每个磁盘并向终端打印一条消息,标识磁盘、中心、半径和 ID。但是,每次我单击磁盘时,都会触发所有 磁盘的拾取事件。我哪里错了?

这是剧情

enter image description here

这是点击磁盘 1 的输出

enter image description here

这是代码。

from global_config import GC
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np


class Disk:


def __init__(self, center, radius, myid = None, figure=None, axes_object=None):
"""
@ARGS
CENTER : Tuple of floats
RADIUS : Float
"""
self.center = center
self.radius = radius
self.fig = figure
self.ax = axes_object
self.myid = myid

def onpick(self,event):
print "You picked the disk ", self.myid, " with Center: ", self.center, " and Radius:", self.radius


def mpl_patch(self, diskcolor= 'orange' ):
""" Return a Matplotlib patch of the object
"""
mypatch = mpl.patches.Circle( self.center, self.radius, facecolor = diskcolor, picker=1 )

if self.fig != None:
self.fig.canvas.mpl_connect('pick_event', self.onpick) # Activate the object's method

return mypatch



def main():

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on disks to print out a message')

disk_list = []

disk_list.append( Disk( (0,0), 1.0, 1, fig, ax ) )
ax.add_patch(disk_list[-1].mpl_patch() )

disk_list.append( Disk( (3,3), 0.5, 2, fig, ax ) )
ax.add_patch(disk_list[-1].mpl_patch() )

disk_list.append( Disk( (4,9), 2.5, 3, fig, ax ) )
ax.add_patch(disk_list[-1].mpl_patch() )


ax.set_ylim(-2, 10);
ax.set_xlim(-2, 10);

plt.show()


if __name__ == "__main__":
main()

最佳答案

有很多方法可以解决这个问题。我已经修改了您的代码以尝试显示两种可能的方式(因此它具有无关的代码,尽管您希望以哪种方式处理它)。

一般来说,我认为您只需附加一个 pick_event 处理程序,并且该处理程序需要确定哪个对象被击中。下面的代码以这种方式工作on_pick 函数捕获您的磁盘和补丁,然后返回一个函数来判断单击了哪个数字。

如果您想坚持附加多个 pick_event 处理程序,您可以通过调整 Disk.onpick 来实现,如下所示以确定 pick 事件是否相关到这个磁盘(每个磁盘都会得到每个选择事件)。您会注意到 Disk 类在 self.mypatch 中保存了它的补丁以使其工作。如果您想这样做,请放弃我对 main 所做的所有更改,并取消注释 Disk.mpl_patch 中的两行。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np


class Disk:


def __init__(self, center, radius, myid = None, figure=None, axes_object=None):
"""
@ARGS
CENTER : Tuple of floats
RADIUS : Float
"""
self.center = center
self.radius = radius
self.fig = figure
self.ax = axes_object
self.myid = myid
self.mypatch = None


def onpick(self,event):
if event.artist == self.mypatch:
print "You picked the disk ", self.myid, " with Center: ", self.center, " and Radius:", self.radius


def mpl_patch(self, diskcolor= 'orange' ):
""" Return a Matplotlib patch of the object
"""
self.mypatch = mpl.patches.Circle( self.center, self.radius, facecolor = diskcolor, picker=1 )

#if self.fig != None:
#self.fig.canvas.mpl_connect('pick_event', self.onpick) # Activate the object's method

return self.mypatch

def on_pick(disks, patches):
def pick_event(event):
for i, artist in enumerate(patches):
if event.artist == artist:
disk = disks[i]
print "You picked the disk ", disk.myid, " with Center: ", disk.center, " and Radius:", disk.radius
return pick_event


def main():

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on disks to print out a message')

disk_list = []
patches = []

disk_list.append( Disk( (0,0), 1.0, 1, fig, ax ) )
patches.append(disk_list[-1].mpl_patch())
ax.add_patch(patches[-1])

disk_list.append( Disk( (3,3), 0.5, 2, fig, ax ) )
patches.append(disk_list[-1].mpl_patch())
ax.add_patch(patches[-1])

disk_list.append( Disk( (4,9), 2.5, 3, fig, ax ) )
patches.append(disk_list[-1].mpl_patch())
ax.add_patch(patches[-1])

pick_handler = on_pick(disk_list, patches)

fig.canvas.mpl_connect('pick_event', pick_handler) # Activate the object's method

ax.set_ylim(-2, 10);
ax.set_xlim(-2, 10);

plt.show()


if __name__ == "__main__":
main()

关于python - 正确使用 Matplotlib 的 pick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com