我怎么能得到这些数据?它的文件扩展名是.xvg

时间:2017-02-10 05:07:35

标签: python matplotlib

我是Python新手。我试过这个脚本,但它不起作用。 它给了我这个错误:

Traceback (most recent call last):
    File "temp.py", line 11, in <module>
     y = [row.split(' ')[1] for row in data]
    File "temp.py", line 11, in <listcomp>
    y = [row.split(' ')[1] for row in data]
IndexError: list index out of range

脚本是:

import numpy as np
import matplotlib.pyplot as plt

with open("data.xvg") as f:
    data = f.read()
    data = data.split('\n')

x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[1] for row in data]

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title("Plot title...")    
ax1.set_xlabel('your x label..')
ax1.set_ylabel('your y label...')
ax1.plot(x,y, c='r', label='the data')
leg = ax1.legend()
plt.show()    

数据是:

    0.000000  299.526978
    1.000000    4.849206
    2.000000    0.975336
    3.000000    0.853160
    4.000000    0.767092
    5.000000    0.995595
    6.000000    0.976332
    7.000000    1.111898
    8.000000    1.251045
    9.000000    1.346720
   10.000000    1.522089
   11.000000    1.705517
   12.000000    1.822599
   13.000000    1.988752
   14.000000    2.073061
   15.000000    2.242703
   16.000000    2.370366
   17.000000    2.530256
   18.000000    2.714863
   19.000000    2.849218
   20.000000    3.033373
   21.000000    3.185251
   22.000000    3.282328
   23.000000    3.431681
   24.000000    3.668798
   25.000000    3.788214
   26.000000    3.877117
   27.000000    4.032224
   28.000000    4.138007
   29.000000    4.315784
   30.000000    4.504521
   31.000000    4.668567
   32.000000    4.787213
   33.000000    4.973860
   34.000000    5.128736
   35.000000    5.240545
   36.000000    5.392560
   37.000000    5.556009
   38.000000    5.709351
   39.000000    5.793169
   40.000000    5.987224
   41.000000    6.096015
   42.000000    6.158622
   43.000000    6.402116
   44.000000    6.533816
   45.000000    6.711002
   46.000000    6.876793
   47.000000    7.104519
   48.000000    7.237456
   49.000000    7.299352
   50.000000    7.471975
   51.000000    7.691428
   52.000000    7.792002
   53.000000    7.928269
   54.000000    8.014977
   55.000000    8.211984
   56.000000    8.330894
   57.000000    8.530197
   58.000000    8.690166
   59.000000    8.808934
   60.000000    8.996209
   61.000000    9.104818
   62.000000    9.325309
   63.000000    9.389288
   64.000000    9.576900
   65.000000    9.761865
   66.000000    9.807437
   67.000000   10.027261
   68.000000   10.129250
   69.000000   10.392891
   70.000000   10.497618
   71.000000   10.627769
   72.000000   10.811770
   73.000000   11.119184
   74.000000   11.181286
   75.000000   11.156842
   76.000000   11.350290
   77.000000   11.493779
   78.000000   11.720265
   79.000000   11.700112
   80.000000   11.939404
   81.000000   12.293530
   82.000000   12.267791
   83.000000   12.394929
   84.000000   12.545286
   85.000000   12.784669
   86.000000   12.754122
   87.000000   13.129798
   88.000000   13.166340
   89.000000   13.389514
   90.000000   13.436648
   91.000000   13.647285
   92.000000   13.722875
   93.000000   13.992217
   94.000000   14.167837
   95.000000   14.320843
   96.000000   14.450310
   97.000000   14.515556
   98.000000   14.598526
   99.000000   14.807360
  100.000000   14.982592
  101.000000   15.312892
  102.000000   15.280009

1 个答案:

答案 0 :(得分:0)

尝试以下操作,您需要在附加它们之前将每个值转换为浮点数:

import numpy as np
import matplotlib.pyplot as plt

x, y = [], []

with open("data.xvg") as f:
    for line in f:
        cols = line.split()

        if len(cols) == 2:
            x.append(float(cols[0]))
            y.append(float(cols[1]))


fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title("Plot title...")    
ax1.set_xlabel('your x label..')
ax1.set_ylabel('your y label...')
ax1.plot(x,y, c='r', label='the data')
leg = ax1.legend()
plt.show()  

这会给你一个如下图:

Graph screenshot

获取错误的原因可能是因为您的文件中某处有空行。通过检查分割后的条目数是2,可以确保您不会出现超出范围的索引错误。