0

I am using Jupyter Notebook on a remote server (using ssh). I am using matplotlib and I want to save coordinates to annotate pictures to draw bounding boxes.
I used :

collector = []
def onclick(event):
    global i, collector
    collector.append((event.xdata, event.ydata))
    # Open the annotations file to continue to write
    target = open('annotation.txt', 'a')
    # Write picture and coordinates
    target.write(line)
    target.write("\n")
    i += 1
    event.canvas.figure.clear()
    event.canvas.figure.gca().imshow(images[i])

fig = plt.figure(figsize=(5,5))
fig.canvas.mpl_connect('button_press_event', onclick)

plt.imshow(images[0])
plt.show()

However, I don't why but it seems to not work anymore. Does someone know a solution to save click events on Jupyter Notebook ?
Thank you very much in advance.

A. Attia
  • 1,630
  • 3
  • 20
  • 29

1 Answers1

1

You are probably using the %matplotlib inline backend, which does not support any kind of events (since it produces simple png images). In order to work with interactive elements and events, you would want to use the %matplotlib notebook backend. This means you should put the line

%matplotlib notebook

somewhere upfront your notebook.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Indeed but I don't know why I have to add `%matplotlib notebook` in the cell, it isn't working when I am using `%matplotlib notebook`at the top of my notebook – A. Attia Jan 14 '18 at 09:00
  • You mean each cell needs its own setting of `%matplotlib notebook`? That is quite unusual, normally it should be valid for the complete notebook if put on top of it. – ImportanceOfBeingErnest Jan 14 '18 at 12:08