2017-03-22 2 views
0

다음은 하나의 디렉토리에서 파일을 호출 할 수있는 스크립트입니다. '-o'를 사용하여 명령 줄에서 파일을 선택할 수 있습니다.다른 디렉토리의 여러 파일, 한 그림에 플롯

어떻게 여러 디렉터리에서 여러 파일을 호출하고 같은 그림을 그릴 수 있습니다.

import numpy as np 
import matplotlib.pyplot as plt 
import csv 
import argparse 

parser = argparse.ArgumentParser() 
parser = argparse.ArgumentParser(description='This script only works for gromacs rms files and filename should be in 1.xvg, 2.xvg format') 

parser.add_argument("-o","--input", help="output as PDF.") 


args = parser.parse_args() 
input = (args.input) 
x, y = [],[] 
title = "RMSD" 
xlabel = "Time (ns)" 
ylabel = "RMSD (nm)" 

with open(input) as f: 
     for line in f: 
      cols = line.split() 
      if cols[0][0] == "#": 
       pass 
      elif cols[0][0] == "@": 
       pass 
      else: 
        try: 
         if len(cols) == 2: 
           x.append(float(cols[0])) 
           y.append(float(cols[1])) 
        except ValueError: 
         pass   

fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax1.plot(x,y) 
ax1.set_title(title) 
ax1.set_xlabel(xlabel) 
ax1.set_ylabel(ylabel) 
plt.savefig('data.png', dpi=500) 

답변

0
쉼표로 스크립트 (거기에 공백) 호출 할 때

당신은 단순히 파일의 목록을 지정할 수 있습니다 :

python myscript.py -o file1,file2,file3 

그리고이 다음 스크립트에 추가 :

inputs = (args.input).split(',') 

모든 입력 파일을 반복하면됩니다.

for input in inputs: 
    #Your code here 

각 반복에서 새 축을 만들지 않도록 루프 앞에 축을 만들어야합니다.

관련 문제