0

I have a script that takes data form an external source, runs some analysis on it, and then plots a graph in a separate window using matplotlib.pyplot. I'm running this from iPython in the windows command prompt (with py34.3 activated). The plot looked perfect when I just wanted to print one time, but now I'm trying to automate the updating process... In a while loop, I'm loading new data and printing a graph. My issue is that plt.show() blocks the rest of my code from executing. I've tried using plt.ion() and a few other things but none of it seems to work. For example, with plt.ion(), my plot is not formatted correctly, and after printing the first time, it then freezes and stops responding, so I have to close it and quit the program.

I've checked all over google and stackoverflow and generally am just a little confused about how to apply the answers I've found to my own code. The most viable options I've found are either using a subprocess, using matplotlib animation, or just switching to ggplot2 instead of matplotlib.

Here's my code, I've commented out a few places where I added plt.ion() and things like that:

def display_script(args):    
    fig = plt.figure()
    a_plt = fig.add_subplot(2,1,1)
    a_plt.set_title('A')
    b_plt = fig.add_subplot(2,1,2)
    b_plt.set_title('B') 
    plt.tight_layout()
    #tried adding plt.ion() here but no dice
    plt.ion()
    plt.show()
    plt_dict = {'A':a_plt,'B':b_plt}
    curtime = datetime.datetime.now().time()  
    while curtime < datetime.time(14,1): 
        cur_df = current_data()
        for curchoice in ['A','B']:
            for label in some_labels: 
                val,proj,diffs,style = #some calculations
                plt_dict[curchoice].plot(proj,color='b',linestyle=style,label=label)
                #plt.pause(0.000001)
            val = #some calculations
            plt_dict[curchoice].plot(val,'-r',label='Current Value')
            #plt.pause(0.000001)
            plt_dict[curchoice].legend(loc = 'lower right')
            #plt.pause(0.000001)
            if curchoice in GLOBAL_LIST: 
                plt_dict[curchoice].set_xticklabels(MONTH_LIST[1:])
                #plt.pause(0.000001)
            else: 
                plt_dict[curchoice].set_xticklabels(MONTH_LIST)
                #plt.pause(0.000001)
            txt = ""
            fig = plt_dict[curchoice]
            for mnth in MONTH_LIST: 
                this_label = curchoice + '_' + mnth
                if this_label in yest_data.keys(): 
                    curval = cur_df[this_label][0]
                    diff = curval - yest_data[this_label]
                    txt += "{0}: {1:.2f} {2:.2f}\n".format(this_label,curval,diff)
            if curchoice == 'A': 
                a_plt.text(9.2,7,txt)
                #plt.pause(0.000001)
            else: 
                a_plt.text(9.2,2,txt)
                #plt.pause(0.000001)
            if curchoice=='A':
                txt = "  {0:%H:%M:%S} - FLAGS: \n \n".format(curtime)
                for i in some_range: 
                    txt += " example text {0}\n".format(i)
                a_plt.text(0,1,txt)
                #plt.pause(0.000001)
        plt.subplots_adjust(bottom=0.2,right=0.7)    
        plt.draw()
        #plt.ion()
        #plt.show()#block=False)
        time.sleep(120)
        #plt.close()
        plt.clf()
        #plt.ioff()
        curtime = datetime.datetime.now().time()
    return 
Peter M
  • 1
  • 1
  • what happens if you comment out plt.clf()? – mauve Oct 19 '16 at 20:51
  • @mauve - the data I'm loading isn't available after 4pm, so I'll have to try this first thing in the morning, thanks for the response though! – Peter M Oct 19 '16 at 21:04
  • Have you seen the discussion on this question? http://stackoverflow.com/questions/12670101/matplotlib-ion-function-fails-to-be-interactive – user812786 Oct 19 '16 at 21:23
  • @whrrgarbl yes I did, you'll see the commented-out plt.pause statements in my code. It didn't work. – Peter M Oct 20 '16 at 11:26
  • @mauve I tried taking plt.clf() out and it just kept plotting more and more data on top of the same graph, which I don't want... also the problem remains of, as soon as I click on the window, it becomes unresponsive – Peter M Oct 20 '16 at 13:00

0 Answers0