【问题标题】:multiple graphs in one fig from different input files来自不同输入文件的一个图中的多个图表
【发布时间】:2017-03-24 21:21:59
【问题描述】:

我不知道我的脚本哪里出了问题。我打开了三个文件,它绘制了三个文件,但图形之间有一些连接线。在我看来,有连续的路线。谁能帮帮我?

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)

运行我的脚本后的图像:

【问题讨论】:

  • 所有点都在一个数组中。绘制点时,它们将全部连接。 (matplotlib 应该如何知道在哪一点不连接它们?)最系统的方法是每个文件有一对 x.y 数组并为每个文件发出一个绘图命令。
  • 尊敬的先生,问题是,我不知道该怎么做。

标签: python-3.x csv matplotlib


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2016-11-02
    相关资源
    最近更新 更多