Figure GUI freezing

雨燕双飞 提交于 2019-11-27 08:36:50

问题


I am fairly new in python, and I am trying to have a plot, based on data stored in a file. This file may be updated at any time, so I am trying to make the drawing updated every 3 seconds (so I don't use all the CPU). My problem is that the GUI freezes after the lunch.

#!/usr/bin/python
# _*_ coding: utf8 _*_

import matplotlib.pyplot as plt
import numpy as np
import time 

plt.ion()
plt.figure()
i=0
while 1:
    taille=0
    fichier=np.loadtxt('data/US.SAVE')
    fichier1=np.loadtxt('data/cond.SAVE')
    taille1=np.size(fichier1[:,1])
    taille=np.size(fichier[:,1])

    min=min(fichier[0,0],fichier1[0,0]);

    fichier[:,0]=fichier[:,0]-min
    fichier1[:,0]=fichier1[:,0]-min


    if (taille != taille1) :
        printErrors("TAILLE DE FICHIERS DIFFERENTES")


    nb_chunks=np.size(fichier1[1,:])
    nb_inputs=np.size(fichier[1,:])


    plt.subplot(3,1,1)

    plt.bar(fichier[:,0],fichier[:,1],align='center',width=0.0001, facecolor='b', label="US")
    x1,x2,y1,y2 = plt.axis()
    x1=x1-0.0001
    plt.axis([x1, x2, y1, 1.2])
    plt.legend(ncol=3,prop={'size':9})
    plt.title("US ") 
    plt.ylabel('Activation')
    plt.xlabel('Time')

    plt.subplot(3,1,2)

    plt.bar(fichier1[:,0],fichier1[:,1],align='center',width=0.0001, facecolor='b', label="response")



    plt.axis([x1, x2, y1, 1.2])
    plt.legend(ncol=3,prop={'size':9})
    plt.title("Response ") 
    plt.ylabel('Activation')
    plt.xlabel('Time')


    plt.subplot(3,1,3)

    plt.bar(fichier[:,0]-fichier1[:,0],fichier1[:,1],align='center',width=0.0001, facecolor='b', label="Error")
    plt.axis([x1, x2, y1, 1.2])
    plt.legend(ncol=3,prop={'size':9})
    plt.title("Error") 
    plt.ylabel('Activation')
    plt.xlabel('Time')
    plt.draw()
    name1='data/Conditionnement.eps'
    plt.savefig(name1,dpi=256)
    plt.draw()
    del fichier,fichier1,min
    i=i+1

    time.sleep(3)   

plt.show()

I did not find any other topic on a file based drawing.


回答1:


You want to use the plt.pause(3) function instead of time.sleep(). pause includes the necessary calls to the gui main loop to cause the figure to re-draw.

also see: Python- 1 second plots continous presentation, matplotlib real-time linear line, pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program runs,




回答2:


On top of the answer of @tcaswell (that solve the problem), I suggest to rethink the script in a more OO way.

I have tried this:

plt.ion()
plt.figure()
plt.show()

while True:
    x=np.arange(10)
    y=np.random.rand(10)

    plt.subplot(121)
    plt.plot(x,y)
    plt.subplot(122)        
    plt.plot(x,2*y)

    plt.draw()

    plt.pause(3)

but it does not work (it looks like it opens a gui at plt.figure and then at each loop.

A solution like this:

plt.ion()
fig, ax = plt.subplots(nrows=2, ncols=1)
plt.show()

while True:
    x=np.arange(10)
    y=np.random.rand(10)

    ax[0].plot(x,y)
    ax[1].plot(x,2*y)

    plt.draw()

    plt.pause(3)

is much more efficient (axes are created only once), neater (at the end matplotlib is OO) and potentially less prone to memory leaks.

Besides, from your most I gather that at each loop you read in the files again and then plot the new lines. If this is the case, you want to clear first the content of the axes before redrawing. In my simple case you can clear the axes with

for a in ax:
    a.clear()


来源:https://stackoverflow.com/questions/14647491/figure-gui-freezing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!