首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matplotlib底图:弹出框

Matplotlib底图:弹出框
EN

Stack Overflow用户
提问于 2012-07-18 16:38:09
回答 1查看 6.3K关注 0票数 7

我想知道如何在底图图中创建弹出框。当我将鼠标悬停在某个位置上时,应该会触发弹出框。

这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-19 15:46:19

是的,多亏了matplotlib的事件处理框架,这是可能的。我找不到一个已经写好的例子来做你特别感兴趣的事情,所以我写了一个(我将把它放在matplotlib源码中)。

我会仔细阅读http://matplotlib.sourceforge.net/users/event_handling.html,以便更好地了解正在发生的事情。请注意,尽管"pick_event“听起来像是完美的解决方案,但它是用于鼠标单击的-not,用于鼠标悬停事件,在这种情况下不起作用。

我的代码可以很好地对象化,如下所示:

代码语言:javascript
复制
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes()


points_with_annotation = []
for i in range(10):
    point, = plt.plot(i, i, 'o', markersize=10)

    annotation = ax.annotate("Mouseover point %s" % i,
        xy=(i, i), xycoords='data',
        xytext=(i + 1, i), textcoords='data',
        horizontalalignment="left",
        arrowprops=dict(arrowstyle="simple",
                        connectionstyle="arc3,rad=-0.2"),
        bbox=dict(boxstyle="round", facecolor="w", 
                  edgecolor="0.5", alpha=0.9)
        )
    # by default, disable the annotation visibility
    annotation.set_visible(False)

    points_with_annotation.append([point, annotation])


def on_move(event):
    visibility_changed = False
    for point, annotation in points_with_annotation:
        should_be_visible = (point.contains(event)[0] == True)

        if should_be_visible != annotation.get_visible():
            visibility_changed = True
            annotation.set_visible(should_be_visible)

    if visibility_changed:        
        plt.draw()

on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)

plt.show()

希望所有的东西都是可读的。代码的高级概述如下:

  • 创建一个点,注解对的列表,在默认情况下,注解不是visible
  • Register a function,“

”,每次检测到鼠标运动时都要调用

  • on_move函数迭代通过每个点和注解,如果鼠标现在在其中一个点上,使其相关注解可见,如果不是,则使其不可见。(包含方法为documented here)

票数 31
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11537374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档