来自不同目录的多个文件,绘制在一张图中
multiple files from different directory, plot in one figure
这是我的脚本,它可以从一个目录调用一个文件。我可以使用“-o”从命令行 select 文件。
如何从多个目录中调用多个文件并在同一个图形中绘制。
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)
您可以在使用逗号调用脚本时简单地指定一个文件列表(其中没有空格):
python myscript.py -o file1,file2,file3
然后在您的脚本中添加:
inputs = (args.input).split(',')
并简单地遍历所有输入文件,例如
for input in inputs:
#Your code here
请注意,您应该在循环之前创建轴,这样您就不会在每次迭代中创建新轴。
这是我的脚本,它可以从一个目录调用一个文件。我可以使用“-o”从命令行 select 文件。
如何从多个目录中调用多个文件并在同一个图形中绘制。
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)
您可以在使用逗号调用脚本时简单地指定一个文件列表(其中没有空格):
python myscript.py -o file1,file2,file3
然后在您的脚本中添加:
inputs = (args.input).split(',')
并简单地遍历所有输入文件,例如
for input in inputs:
#Your code here
请注意,您应该在循环之前创建轴,这样您就不会在每次迭代中创建新轴。