python - 如何将事件连接到 matplotlib.collections.PatchCollection
问题描述
使用matplotlib.collections.PatchCollection
,我创建了一个显示六边形网格的 pyplot 图:
现在,我想让情节具有交互性,即添加某种形式的事件处理。特别是,我希望能够将光标悬停在任何六边形上,并且当我这样做时,我想用某种颜色填充所有相邻的六边形。
但是,让我们从简单的开始:如何将补丁(即六边形)连接到事件?
我已将每个六边形的中心点的坐标存储在一个 numpy 数组中。所以,我需要一种方法来告诉我点击的六边形的索引,或者我的光标当前已经结束。总共有 100 个六边形。当我点击 43 号六边形时,我只需要得到这个索引,然后,我想我知道如何获得与所有邻居的距离。但是我如何获得这个索引呢?
有人知道吗?
解决方案
PatchCollection
与所有集合一样,有一个属性contains()
可以告诉您触发事件的是集合的哪个成员(如果有)。
唯一的“技巧”是你必须确保你最初有一个与你的集合中成员数量相同大小的 facecolors 数组,否则事情会变得一团糟。在这里,我PathCollection.set_facecolors()
在创建后使用以确保这一点。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
def hover(event):
if event.inaxes == ax:
cont, ind = p.contains(event)
if cont:
idx = ind['ind'][0]
colors = p.get_facecolors()
colors[idx] = highlight_color
p.set_facecolors(colors)
else:
p.set_facecolors([default_color] * N)
fig.canvas.draw()
default_color = (0, 0, 1, 1)
highlight_color = (1, 0, 0, 1)
N = 10
r = 1
x, y = np.random.randint(10, 50, size=(2, N))
patches = [Circle((xi, yi), r) for xi, yi in zip(x, y)]
p = PatchCollection(patches)
p.set_facecolors([default_color] * N)
fig, ax = plt.subplots()
ax.add_collection(p)
ax.set_xlim(0, 50)
ax.set_ylim(0, 50)
fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()
推荐阅读
- node.js - 如何在 MongoDB 中设置缓冲区偏移范围,它不允许在 BSON 对象中上传超过 16MB 的文件?
- android - Android - 希伯来文文本显示不正确
- reactjs - 如何在使用 reactjs 构建的 jest Shallow 测试中修复组件变量的类型推断?
- java - DDB 中复合范围键的 Java 注释
- getstream-io - 使用 Node js 客户端 (GetStream.io) 的空更新频道请求
- gtk - 使用 vala 编程语言,你能检测到其他应用程序何时全屏或最大化,并暂停自己的程序吗?
- css - 为什么这个 CSS 选择器没有返回任何结果?
- mysql - 为什么查询集合名称,在新 pod 启动后变慢
- swift - Streamchat.io:访问用户的团队
- java - 为什么 Apache Tika 将 jar 文件的 mimetype 检测为 application/zip 而不是 application/java-archive?