1

I have a sequence of images that I am trying to display them using a figure/plot in matplotlib. Also I want to allow the users to navigate between images by clicking on next and previous buttons that I have created using matplotlib.widgets. Here is my code:

class Index(object):
   ind = 0

   def next(self, event):
      self.ind += 1
      orig_frame = cv2.imread(FFMPEG_PATH + all_frames[self.ind])
      ax.imshow(orig_frame)                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
      fig.canvas.draw()
      plt.waitforbuttonpress()


   def prev(self, event):
      self.ind -= 1
      if self.ind >= 0:
         orig_frame = cv2.imread(FFMPEG_PATH + all_frames[self.ind])
         ax.imshow(orig_frame)                                                                                                                                                                                                                                                                                                                                                                                   
         fig.canvas.draw()
         plt.waitforbuttonpress()

fig, ax = plt.subplots()
callback = Index()                                                                                                                                                                                               
start_frame = 0
orig_frame = cv2.imread(FFMPEG_PATH + all_frames[start_frame])
ax.imshow(orig_frame)

axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
next_button = Button(axnext, 'Next')
next_button.on_clicked(callback.next)
prev_button = Button(axprev, 'Previous')
prev_button.on_clicked(callback.prev)


plt.show()
plt.waitforbuttonpress()

The problem is that, although that I have put fig.canvas.draw() and plt.waitforbuttonpress() at the end of next and prev functions, the program terminates as soon as I click next button. Any idea what is the problem?

user2308191
  • 281
  • 1
  • 7
  • 18
  • Why does the `next` function take in an `event` argument? I don't think you're passing it when you call it. Have you checked that the code is making it into the `next` function with a `print` statement maybe? – mitoRibo Jul 27 '16 at 21:50
  • How are you running this? Just `python script.py` or from inside IPython or a notebook? The reason I ask is because 'interactive mode' seems to be on. You want to switch it off (e.g. `plt.ioff()`), but the exact method of doing that depends on how you are running the script. – Marijn van Vliet Jul 28 '16 at 08:01

0 Answers0