来自不同输入文件的一个图中的多个图形

时间:2017-03-24 21:21:59

标签: python-3.x csv matplotlib

我不知道我的脚本中的问题在哪里。我打开了三个文件,它绘制了三个文件,但图中有一些连接线。在我看来有连续的线。任何人都可以帮我解决这个问题吗?

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


x, y = [],[]
title = "RMSD"
xlabel = "Time (ns)"
ylabel = "RMSD (nm)"

for input in inputs:
    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 ,'r--', label='%s'%input)
legend = ax1.legend(loc='best', shadow=True)
ax1.set_title(title)  
ax1.set_xlabel(xlabel)  
ax1.set_ylabel(ylabel)
plt.savefig('data.png', dpi=500)

运行我的脚本后的图像:
enter image description here

1 个答案:

答案 0 :(得分:0)

一个选项是在循环中绘制图形,每个输入文件一个绘图命令。

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

x, y = [],[]
title = "RMSD"
xlabel = "Time (ns)"
ylabel = "RMSD (nm)"

fig = plt.figure()
ax1 = fig.add_subplot(111)

for inp in inputs:
    x, y = [],[]
    with open(inp) as f:
        for line in f:
            cols = line.split()
            if cols[0][0] not in ["#","@"]:
                try:
                    if len(cols) == 2:
                        x.append(float(cols[0]))
                        y.append(float(cols[1]))
                except ValueError:
                      pass
    ax1.plot(x,y ,'r--', label='%s'%inp)



legend = ax1.legend(loc='best', shadow=True)
ax1.set_title(title)  
ax1.set_xlabel(xlabel)  
ax1.set_ylabel(ylabel)
plt.savefig('data.png', dpi=500)