IPYTHON脚本,它接受任意数量的输入文件,条带+存储标题,并保存数据点

时间:2017-09-17 05:18:36

标签: matplotlib ipython

我试图在命令行中指定任意数量的文件(1-10为简单?),剥离+存储标题,并保存数据点(然后使用MATPLOT绘制它。最后一个命令行指定的名称将是输出文件的名称。

下面是一个脚本,它接受2个命令行指定文件并绘制它们,第3个命令行名称指定输出文件的名称。

import sys
sys.argv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
import numpy as np

# Reads Arguements
ReadFile1 = sys.argv[1]
ReadFile2 = sys.argv[2]
WriteFile = sys.argv[3]


# opens file
RedFile1 = open(ReadFile1)
RedFile2 = open(ReadFile2)


###
### Start collecting data ###
###


# Removes header from file 1 and stores it
header1 = RedFile1.readline().rstrip()

# Creates a list from values in file 1 , reads argument specified file,
# puts data into the list, and converts into an array.
Data1 = []

for line in RedFile1:
     Data1.append(line.rstrip().split(','))
Reference1 = np.array(Data1).astype(np.float)

# Seperates array into x and y values in the first document
# based on the columb it is in
xvalues1 = Reference1[:, 0]
yvalues1 = Reference1[:, 1]


######
###### moving on to the second part ######
######


# Removes header from file 2, stores it, and puts it in an array.
header2 = RedFile2.readline().rstrip()

# Creates a list from values in file 2 , reads argument specified file,
# puts data into the list, and converts into an array.
Data2 = []

for line in RedFile2:
    Data2.append(line.rstrip().split(','))
Reference2 = np.array(Data2).astype(np.float)

# Seperates array into x and y values in the second document
# based on the columb it is in
xvalues2 = Reference2[:,0]
yvalues2 = Reference2[:,1]


######
###### Graphing ######
######


# Spits out a graph of file 1 and 2 together
line1 = plt.plot(xvalues1, yvalues1, 'o-', label = header1)
line2 = plt.plot(xvalues2, yvalues2, 'o-', label = header2)
plt.ylabel('Cell Count')
plt.xlabel('Time (min)')

plt.legend(numpoints=1, loc=0)


# Saves the graph into the argument specified name
plt.savefig(WriteFile)

我的可怜尝试看起来像:

import sys
sys.argv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

# Takes arguments, places them in list rawr, then the for loop opens each file, strips
# the header,  and places the headers into allheaders
rawr = sys.argv[1:-1]

#Write file name
WriteFile = sys.argv[-1]

#Opens a file, removes header and stores it, stores data in an array
#The array is accessed to create a list of x adn y values
#plot is constructed for one file
#loop continues until no more files


for n in rawr:
    open(n)
    header = n.readline().rstrip()

    Data = []
    for line in n:
        Data.append(line.rstrip().split(','))
    Reference1 = np.array(Data1).astype(np.float)

    xvalues = Reference[:, 0]
    yvalues = Reference[:, 1]

    plt.plot(xvalues, yvalues, label=header)


# Spits out a graph of file(s) together
plt.ylabel('Cell count')
plt.xlabel('Time min')
plt.legend(numpoints=1, loc=0)


# Saves the graph into the argument specified name
plt.savefig(WriteFile + '.png')

EDIT !!!!编辑!!!!!编辑!!!!编辑!!!!!编辑!!!!编辑!!!!!编辑!!!!编辑!!!!!编辑!!!!编辑!!!!!编辑!!!!编辑!!!!!编辑!!!!编辑!!!!!编辑!!!!编辑!!!!!我想通了,这是答案,for循环应该是这样的:

for n in rawr:
    eachfile = open(n)
    header = eachfile.readline().rstrip()

    Data = []
    for line in eachfile:
        Data.append(line.rstrip().split(','))
    Reference = np.array(Data).astype(np.float)

    xvalues = Reference[:, 0]
    yvalues = Reference[:, 1]

    line = plt.plot(xvalues, yvalues, 'o-', label=header)

0 个答案:

没有答案