了解matplotlib事件处理:什么是event和mpl_connect? [英] Understanding matplotlib event handling: what are event and mpl_connect?

查看:107
本文介绍了了解matplotlib事件处理:什么是event和mpl_connect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让在我的散点图中按下一个点时显示值成为可能.解决方案在这里找到:可能将鼠标悬停在 matplotlib 中的点上时是否显示标签?

I wanted to make it possible to show values when pressing a dot in my scatterplot. The solution was found here: Possible to make labels appear when hovering over a point in matplotlib?

解决方案:

from matplotlib.pyplot import figure, show
import numpy as npy
from numpy.random import rand


# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)

x, y, c, s = rand(4, 100)
def onpick3(event):
    ind = event.ind
    print 'onpick3 scatter:', ind, npy.take(x, ind), npy.take(y, ind)

fig = figure()
ax1 = fig.add_subplot(111)
col = ax1.scatter(x, y, 100*s, c, picker=True)
#fig.savefig('pscoll.eps')
fig.canvas.mpl_connect('pick_event', onpick3)

show()

它解决了我的问题.但我不知道该怎么做,我一直在四处搜寻,没有任何运气.我知道如何使用matplotlib进行绘图,所以这也不是我所缺乏的.

And it solved my problem. But I don't understand how, I've been googling around without any luck. I know how to plot with matplotlib, so that's not where my knowledge is lacking.

我不了解的一件事是 onpick3(event)函数.这个事件参数是什么?因为该函数本身是在没有任何给定参数的情况下进一步调用的: fig.canvas.mpl_connect('pick_event',onpick).

One thing I don't understand is the onpick3(event) function. What is this event parameter? Because the function itself is called upon further down without any given arguments: fig.canvas.mpl_connect('pick_event', onpick).

推荐答案

mpl_connect 将信号连接到插槽.在这种情况下,插槽是 onpick3.
请注意,未调用该插槽,即语法

mpl_connect connects a signal to a slot. The slot is in this case onpick3.
Note that the slot is not called, i.e. the syntax is

fig.canvas.mpl_connect('pick_event', onpick3)

not

fig.canvas.mpl_connect('pick_event', onpick3())

仅在触发信号(在画布上单击鼠标)后才调用它.此时,底层 event 作为函数调用中的参数提供.

It will only be called once the signal is triggered (mouse clicked on canvas). At this point the underlying event is provided as an argument in the function call.

一旦您尝试定义不带参数的插槽,您就会看到这一点.这会导致类似 onpick3期望0个参数但得到1个的错误.

You'll see that once you try to define the slot without argument. This would cause an error like onpick3 expects 0 arguments but got 1 or so.

您可以在 matplotlib 事件处理页面上找到详细信息.event 本身是 matplotlib 的一个实例.backend_bases.PickEvent..ind 属性没有很好的文档记录,但这主要是因为并非所有艺术家实际上都将此属性注册到事件中.

You'll find details on the matplotlib event handling page. The event itself is an instance of matplotlib.backend_bases.PickEvent. The .ind attribute is not well documented, but that is mainly because not all artists actually register this attribute to the event.

这篇关于了解matplotlib事件处理:什么是event和mpl_connect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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