更新matplotlib.pyplot文件python中的图形

2024-09-30 14:19:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它从外部源获取数据,对其运行一些分析,然后使用matplotlib.pyplot文件. 我在windows命令提示符下从iPython运行这个程序(激活了py34.3)。当我只想打印一次的时候,这个情节看起来很完美,但是现在我正在尝试自动化更新过程。。。在while循环中,我加载新数据并打印一个图表。我的问题是节目()阻止我的其余代码执行。我试过用血小板()和其他一些东西,但似乎都不起作用。例如,使用血小板(),我的绘图格式不正确,第一次打印后,它会冻结并停止响应,因此我必须关闭它并退出程序。你知道吗

我在google和stackoverflow上都查过了,对于如何将我找到的答案应用到我自己的代码上,我通常有点困惑。我发现最可行的方法是使用子进程、使用matplotlib动画,或者直接切换到ggplot2而不是matplotlib。你知道吗

这是我的代码,我已经注释掉了我添加的一些地方血小板()诸如此类:

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 

Tags: intxtfordatetimetimefigpltsome