我如何打印这些数据?它的文件扩展名是 .xvg

问题描述 投票:0回答:4

我是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
python matplotlib
4个回答
4
投票

如果它是来自 GROMACS 的 xvg 文件,它可能有一些以

@
开头的注释,因此无需编辑该文件即可:

x,y = np.loadtxt("file.xvg",comments="@",unpack=True)
plt.plot(x,y)

unpack=True
使列作为单独的数组出现,并设置为左侧的
x
y
。当然你也可以解析评论来获取标签和图例。


3
投票

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

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()  

这会给你一个看起来像这样的图表:

出现错误的原因可能是因为文件中的某处有空行。通过检查拆分后的条目数是否为

2
,可以确保您不会出现索引超出范围错误。


0
投票

您可以使用 python 库或 windows/linux 可执行文件来绘制 GMXvg 包中的 XVG 文件。

它将发现 XVG 并将其转换为 JPG 或 python 的 matplotlib 支持的任何其他扩展。


0
投票

对于标题和标签以@开头的行以及以#开头的注释的情况,对乔纳坦答案的修改效果很好。

示例 XVG 文件:data.xvg

#此文件创建于 2024 年 4 月 23 日星期二 12:41:52 #创建者:

#:-) GROMACS - gmx 潜力,2022 (-:

@标题“静电势”

@x轴标签“平均坐标(nm)”

@yaxis标签“潜力(V)”

@类型xy@视图

               0                        -0
1.598744988441467                       -0

3.197489976882935                       -0

4.796234607696533       0.2752701044082642

使用 numpy.loadtext() 绘制此图的代码:

更改了评论参数

x,y = np.loadtxt("data.xvg",comments=["#","@"],unpack=True)

plt.plot(x,y)

Graph produced by the code on sample data

© www.soinside.com 2019 - 2024. All rights reserved.