python - How do I flush a graphics figure from matplotlib.pylab when inside a file input loop? -
i using python 2.7 , importing libraries numpy , matplotlib. want read multiple file names of tab-delimited text files (time, voltage , pressure level measurements) , after each 1 display corresponding graph %pylab.
my code can display graph want, after come in specific string ('exit') out of while loop. want see each graph displayed after file name has been entered , have multiple figures on screen @ once. what's wrong code below?
import numpy np import matplotlib.pylab plt filename = '' filepath = 'c:/users/david/my documents/cardiorespiratory experiment/' filenotfounderror = 2 while filename != 'exit': is_valid = false while not is_valid : seek : filename = raw_input('file name:') if filename == 'exit': break fullfilename = filepath+filename+str('.txt') info = np.loadtxt(fullfilename, skiprows=7, usecols=(0,1,2)) is_valid = true except (filenotfounderror, ioerror): print ("file not found") t = [row[0] row in data] v = [row[1] row in data] p = [row[2] row in data] #c = [row[3] row in data] plt.figure() plt.subplot(2, 1, 1) plt.title('graph title ('+filename+')') plt.xlabel('time (s)') plt.ylabel('voltage (mv)') plt.plot(t, v, color='red') plt.subplot(2, 1, 2) plt.xlabel('time (s)') plt.ylabel('pressure (kpa)') plt.plot(t, p, color='blue') plt.show()
i have tried padraic cunningham's suggestion utilize single while loop file name , that's improvement. when set graphing commands within loop, figure comes empty window message "not responding". graph appears in figure after exiting while loop. want figures appear upon getting file name. here's current code:
import numpy np import matplotlib.pylab plt filename = '' filepath = 'c:/users/david/my documents/cardiorespiratory experiment/' filenotfounderror = 2 count = 0 while count <= 4: seek : filename = raw_input('file name:') fullfilename = "{}{}.txt".format(filepath, filename) info = np.loadtxt(fullfilename, skiprows=7, usecols=(0,1,2)) is_valid = true except (filenotfounderror, ioerror): print ("file not found") count += 1 t = [row[0] row in data] v = [row[1] row in data] p = [row[2] row in data] plt.figure() plt.subplot(2, 1, 1) plt.title('graph title ('+filename+')') plt.xlabel('time (s)') plt.ylabel('voltage (mv)') plt.plot(t, v, color='red') plt.subplot(2, 1, 2) plt.xlabel('time (s)') plt.ylabel('pressure (kpa)') plt.plot(t, p, color='blue') plt.show()
just utilize 1 loop filename:
while true: seek : filename = raw_input('file name:') full_filename = "{}{}.txt".format(filepath, filename) info = np.loadtxt(full_filename, skiprows=7, usecols=(0,1,2)) break # if no error break out of loop except (filenotfounderror, ioerror): # else grab error , inquire 1 time again print ("file not found")
".txt"
string no need cast
python graphics matplotlib
No comments:
Post a Comment