Matplotlib底图:弹出框 [英] Matplotlib basemap: Popup box

查看:258
本文介绍了Matplotlib底图:弹出框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to know how to create a popup box in a basemap plot. When I hover my mouse over a location , it should trigger the popup box.

这可能吗?

推荐答案

是的,可以感谢matplotlib的事件处理框架。我找不到已经写过的例子,这样做就是你特别感兴趣的,所以我写了一个(我将提出包含在matplotlib源中)。

Yes it is possible thanks to matplotlib's event handling framework. I couldn't find an already written example which does what you are particularly interested in so I wrote one (which I will put forward for inclusion in the matplotlib source).

我将阅读 http://matplotlib.sourceforge.net/users/event_handling.html 彻底地了解发生了什么。请注意,虽然这听起来像是完美的解决方案pick_event是为了鼠标点击 - 不是鼠标超出事件,在这种情况下不起作用。

I would read http://matplotlib.sourceforge.net/users/event_handling.html thoroughly to best understand what is going on. Please note that although it sounds like the perfect solution "pick_event" is for mouse clicks -not for mouse over- events and doesn't work in this case.

我的代码,可以非常好的客观化,如果想要,看起来像:

My code, which could be objectified very nicely should one want, looks like:

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()

希望一切都应该是相当可读的。代码的高级概述:

Hopefully everything should be fairly readable. A high level overview of the code goes:


  • 创建一个[point,annotation]对的列表,默认情况下,注释不可见

  • 注册一个函数on_move,每次检测到鼠标移动时调用

  • on_move函数遍历每个点和注释,如果鼠标现在超过其中一个点,则使其关联的注释可见,如果不是,则使其不可见。 (包含的方法是在此记录

  • Create a list of [point, annotation] pairs, where by default the annotation is not visible
  • Register a function, "on_move", to be called every time there is mouse motion detected
  • The on_move function iterates through each point and annotation, if the mouse is now over one of the points, make its associated annotation visible, if it is not, make it invisible. (the contains method is documented here)

这篇关于Matplotlib底图:弹出框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆