0

I'm plotting 2 graphs in the same figure inside a 'for' loop. The problem is that, in order to keep the loop running I have to manually close the figure using the mouse. This is difficult as the loop is over 300 steps long. I'm looking for a simpler solution, like clicking the number 1 to close the current figure and view next. I tried with the following code, and it is not working. I believe that the succeeding line is only read after the existing graph is closed. How to fix this?

P.S : Also, using raw_input() for the keypress is not a good way, as I have to additionally hit the 'Enter'. So suggest an alternate method where I can close the graphs by continuously pressing '1' .

for roww in range (0,height) :
        com = 0
        isignal = matrix[roww]
        fft_ith = np.fft.fft(isignal)
        fft_abs_ith = np.abs(fft_ith)
        c_fft = fft_abs_ith[:len(fft_abs_ith)//2] 
        c_fft[zi] = 0


        plt.subplot(2,1,1)
        plt.plot(time,isignal,marker='.')
        plt.xlim(0,time[len(time)-1])
        plt.title("Individual oscilations  and FFT of each cell \n cell ="+str(roww))
        plt.subplot(2,1,2)
        plt.plot(c_freqq,c_fft,marker = ".")
        plt.show()
comnd = raw_input()
if comnd == 1
        plt.close()


brownser
  • 545
  • 7
  • 25

3 Answers3

3

By default matplotlib figures can be closed using the q key. If instead you want to use the 1 key you can add

plt.rcParams["keymap.quit"] = "1"

somewhere on top of your script.

You can set it to plt.rcParams["keymap.quit"] = ['ctrl+w', 'cmd+w', 'q'] or whatever you like.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

The raw_input() function returns a string, so you need to cast comnd to an integer or replace the "1" in your if statement with '1'.

  • Sorry this is still not working, I replaced the if condition with a string '1' and now I have to first manually close the graph using mouse, then press '1' and then hit 'Enter" ! – brownser Feb 11 '19 at 17:04
0

This is not a direct response to your question but could be useful with some modifications. In the code referred, I create a figure and, instead of replotting or closing the window, I update the lines objects created.

gif

Code is at: https://github.com/gustavovelascoh/plot_update

gustavovelascoh
  • 1,208
  • 1
  • 14
  • 28