onclick 函数 matplotlib [英] onclick function matplotlib

查看:53
本文介绍了onclick 函数 matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmap = mpl.colors.ListedColormap([[1,0,0], [0,0,1], [0,1,0], [1,1,0]])

    self.fig = plt.figure(figsize=(6,6))
self.ax = self.fig.add_axes([0.1, 0.1, 0.8, 0.8])
x_ax = self.fig.add_axes([0.05, 0.1, 0.05, 0.8])
x2_ax = self.fig.add_axes([0.05, 0.1, 0.05, 0.8])
y_ax = self.fig.add_axes([0.1, 0.05, 0.8, 0.05])
x_ax.imshow(xcolors, cmap=cmap, interpolation='none')
x_ax.set_aspect('auto')
x_ax.set_position((0.1,0.1,0.05,0.8))
y_ax.imshow(ycolors, cmap=cmap, interpolation='none')
x_ax.set_picker(5)
def on_pick(event):
    print hello


self.canvas = FigureCanvas(self, -1, self.fig)
self.canvas.mpl_connect('Pick_event',on_pick)

我的 onclick 方法有问题基本上我想点击颜色条并打印出你好使用我拥有的代码,它什么都不做我做错了什么?

I am having trouble with my onclick method basically I want to click on the colorbar and for it to print out hello with the code i have, it does nothing what have I done wrong?

推荐答案

主要问题是您正在创建一个新的 canvas 而不是使用已有的 fig.有(至少)两种方法可以做你想做的事.

The primary problem is that you are creating a new canvas instead of using the one fig already has. There are (atleast) two ways to do what you want.

您可以通过设置图像来响应pick事件来实现这一点(参见本教程 示例 4):

You can do this by setting the images to respond to the pick event (see this tutorial example 4):

cmap = mpl.colors.ListedColormap([[1,0,0], [0,0,1], [0,1,0], [1,1,0]])

xcolors = arange(15).reshape(15,1)
ycolors = arange(15).reshape(1,15)
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
x_ax = fig.add_axes([0.05, 0.1, 0.05, 0.8])
x2_ax = fig.add_axes([0.05, 0.1, 0.05, 0.8])
y_ax = fig.add_axes([0.1, 0.05, 0.8, 0.05])
x_ax.imshow(xcolors, cmap=cmap, interpolation='none', picker=True)
x_ax.set_aspect('auto')
x_ax.set_position((0.1,0.1,0.05,0.8))
y_ax.imshow(ycolors, cmap=cmap, interpolation='none', picker=True)
def on_pick(event):
    artist = event.artist
    if isinstance(artist, matplotlib.image.AxesImage):
        im = artist
        A = im.get_array()
        print A.shape,
    print 'hello'


canvas = fig.canvas
canvas.mpl_connect('pick_event',on_pick)

或者,您可以设置以响应选择事件(请参阅colorbar matplotlib python 上的 onclick 方法) 如此:

Alternatively, you can set the axes to respond to the pick event (see onclick method on a colorbar matplotlib python) as such:

cmap = mpl.colors.ListedColormap([[1,0,0], [0,0,1], [0,1,0], [1,1,0]])

xcolors = arange(15).reshape(15,1)
ycolors = arange(15).reshape(1,15)
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
x_ax = fig.add_axes([0.05, 0.1, 0.05, 0.8])
x2_ax = fig.add_axes([0.05, 0.1, 0.05, 0.8])
y_ax = fig.add_axes([0.1, 0.05, 0.8, 0.05])
x_ax.imshow(xcolors, cmap=cmap, interpolation='none')
x_ax.set_aspect('auto')
x_ax.set_position((0.1,0.1,0.05,0.8))
y_ax.imshow(ycolors, cmap=cmap, interpolation='none')

x_ax.set_picker(5)
y_ax.set_picker(5)

def on_pick(event):
    artist = event.artist
    if isinstance(artist, matplotlib.axes.Axes):

        print event.mouseevent.ydata,
        print event.mouseevent.xdata,
    print 'hello'


canvas = fig.canvas
canvas.mpl_connect('pick_event',on_pick)

请注意活动所携带的艺术家类型的不同(以及您可以轻松访问的信息).

Notice the difference in the type of artist that the event is carrying with it (and hence the information you have easy access to).

这篇关于onclick 函数 matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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