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

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?

1
  • 1
    As general advice, it's better if you post code that people can run with out having to make any modifications (ie striping out all the self and providing reasonable fake data). Commented Feb 17, 2013 at 16:39

1 Answer 1

3

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.

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)

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.