2015-02-05 50 views
0

我試圖使用多個if語句來檢查某個條件,然後在Python中使用matplotlib來繪製一些數據。首先,我在目錄上執行os.walk以獲取文件列表,然後加載它們以最終繪製並保存圖形。這裏是我的代碼:如果Python中的語句問題

def velocity(): 
    plt.xlabel('$\mathrm{Iterations\/or\/Time}$') 
    plt.title(r'$\mathrm{Residual\/history}$') 
    plt.grid(True) 
    plt.yscale('log') 

    if 'Ux_0' in lf: 
     print "Entered" 
     plt.plot(time_x, value_x, color = 'r', label = 'x-vel') 
    elif 'Uy_0' in lf: 
     print "Entered" 
     plt.plot(time_y, value_y, color = 'b', label = 'y-vel') 
    elif 'Uz_0' in lf: 
     print "Entered" 
     plt.plot(time_z, value_z, color = 'g', label = 'z-vel') 

     plt.legend() 
     plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
     plt.close() 

    return (time_x, value_x, lf) 
    return (time_y, value_y, lf) 
    return (time_z, value_z, lf) 

for path, dirs, files in os.walk(logsDir, topdown=False): 
    for lf in files: 
     if 'Ux_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_x = np.loadtxt(logFile) 
      time_x, value_x = data_x[:,0], data_x[:,1] 
      (time_x, value_x, lf) = velocity() 
     if 'Uy_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_y = np.loadtxt(logFile) 
      time_y, value_y = data_y[:,0], data_y[:,1] 
      (time_y, value_y, lf) = velocity() 
     if 'Uz_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_z = np.loadtxt(logFile) 
      time_z, value_z = data_z[:,0], data_z[:,1] 
      (time_z, value_z, lf) = velocity() 

logDir只有三個文件,開始和他們Ux_0Uy_0Uz_0。有趣的是,在os.walk當我printlf,我得到文件的訂單Ux_0Uz_0Uy_0。現在,函數velocity()生成的數字僅具有來自Ux_0Uz_0的數據,而不具有來自Uy_0的數據。然而,在我的函數中,如果Uy_0Uz_0的順序被顛倒,使得我有Uz_0,緊接在Ux_0後面,如下所示,我得到所有三個圖。

if 'Ux_0' in lf: 
    print "Entered" 
    plt.plot(time_x, value_x, color = 'r', label = 'x-vel') 
elif 'Uz_0' in lf: 
    print "Entered" 
    plt.plot(time_z, value_z, color = 'b', label = 'z-vel') 
elif 'Uy_0' in lf: 
    print "Entered" 
    plt.plot(time_y, value_y, color = 'g', label = 'y-vel') 

    plt.legend() 
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
    plt.close() 

return (time_x, value_x, lf) 
return (time_y, value_y, lf) 
return (time_z, value_z, lf) 

我不確定是什麼原因造成的。

+1

爲什麼'velocity'返回變量,它從全球範圍發生,並以任何方式不會改變? – zehnpaard 2015-02-05 23:23:57

回答

1

雖然Padraic對您的return陳述完全正確無誤,但問題的實際原因是您的縮進和放置plt.savefig命令。

如果你看一下,你有你的plt.savefig聲明,它只有當你到達最後elif,即當它發現在lfUz_0執行。當Uz_0是列表中的第二項時,該繪圖僅在該點保存,因此最後一個數據集被繪製但未保存。

您應該有一個save_velocity()函數,您可以在最後運行。

def velocity(): 
    #Rows omitted for succinctness' sake 

    if 'Ux_0' in lf: 
     print "Entered" 
     plt.plot(time_x, value_x, color = 'r', label = 'x-vel') 
    elif 'Uy_0' in lf: 
     print "Entered" 
     plt.plot(time_y, value_y, color = 'b', label = 'y-vel') 
    elif 'Uz_0' in lf: 
     print "Entered" 
     plt.plot(time_z, value_z, color = 'g', label = 'z-vel') 

    return (time_x, value_x, lf) # Do Padraic's fixes here! 
    return (time_y, value_y, lf) 
    return (time_z, value_z, lf) 

def save_velocity(): 
    plt.legend() 
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
    plt.close() 

for path, dirs, files in os.walk(logsDir, topdown=False): 
    for lf in files: 
     #Rows omitted for succinctness' sake 
     if 'Uz_0' in lf: 
      logFile = os.path.join(path, lf) 
      data_z = np.loadtxt(logFile) 
      time_z, value_z = data_z[:,0], data_z[:,1] 
      (time_z, value_z, lf) = velocity() 

save_velocity() 

的代碼位的清理:

def velocity(time, value, lf): 
    plt.xlabel('$\mathrm{Iterations\/or\/Time}$') 
    plt.title(r'$\mathrm{Residual\/history}$') 
    plt.grid(True) 
    plt.yscale('log') 

    if 'Ux_0' in lf: 
     velocity_color = 'r' 
     velocity_label = 'x-vel' 
    elif 'Uy_0' in lf: 
     velocity_color = 'b' 
     velocity_label = 'y-vel' 
    elif 'Uz_0' in lf: 
     velocity_color = 'g' 
     velocity_label = 'z-vel' 

    print "Entered" 
    plt.plot(time, value, color = velocity_color, label = velocity_label) 

    return time, value, lf 

def save_velocity(): 
    plt.legend() 
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100) 
    plt.close() 

for path, dirs, files in os.walk(logsDir, topdown=False): 
    for lf in files: 
     if any((filename in lf) for filename in ('Ux_0', 'Uy_0', 'Uz_0')): 
      logFile = os.path.join(path, lf) 
      data = np.loadtxt(logFile) 
      time, value = data[:,0], data[:,1] 
      (time, value, lf) = velocity(time, value, lf) # Not sure why you return any value at all here, do you use these values later in some way? 

save_velocity() 
+0

謝謝@zehnpaard。這個邏輯完全按照我的需要工作。 – hypersonics 2015-02-06 00:29:15

+0

沒問題@Deepak,很樂意幫忙。 – zehnpaard 2015-02-06 00:38:45

2

你永遠只能返回相同的值,所以如果你在設定值,其餘都是你不可達如果基於想着你是從你的速度函數得到不同的值,你不會:

return (time_x, value_x, lf) 
    return (time_y, value_y, lf) # < unreachable 
    return (time_z, value_z, lf) # < unreachable 

一個函數在它返回一個返回語句時結束,因此只要你到達它的第一個語句就結束。

您可以返回多個元素:

return (time_x,time_y, time_z value_x, value_y, value_z, lf) 

然後用切片分配res = velocity(); a,b,c = res[2],res[3] ,res[4]等提取和任何你想要的分組。

+0

沒錯。應該使用'return'作爲最終輸出。如果你試圖獲得所有的值,你應該使用'print',然後返回一個值,以得到速度函數的點。 – Zizouz212 2015-02-05 23:30:38

+0

謝謝@Padraic。你能詳細說明你的解釋嗎? – hypersonics 2015-02-05 23:57:28

+0

@Deepak,你只有'(time_x,value_x,lf)',你不能有多個返回語句。您可以返回所有元素的元組,併爲返回的元組索引來設置所需的值。 – 2015-02-06 00:01:51