绘制Gromacs的xvg文件的脚本 - Script for plotting Gromacs xvg files

写了一个脚本,将Gromacs产生的xvg文件绘制成png图片。

Wrote a script that converts xvg files generated by Gromacs into png images.

https://github.com/Tennisatw/cchem_scripts/blob/main/analysis/gromacs_xvg_plotting.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Author: Tennisatw
Date: 2024/03/25
Description:

将gromacs产生的xvg文件绘制成png图片。
Plot the xvg file generated by gromacs as a png image.

gromacs会用到一些grace的plotting命令, 参考https://plasma-gate.weizmann.ac.il/Grace/doc/UsersGuide.html
Gromacs will use some grace plotting commands, refer to https://plasma-gate.weizmann.ac.il/Grace/doc/UsersGuide.html
"""

# xvg文件地址 - xvg File address
file_address = r"xxx.xvg"

# 需要绘制的曲线 - Lines to be plotted
# 如需绘制第1,3,5条曲线,填入[1, 3, 5] - If you want to plot the 1st, 3rd, and 5th lines, fill in [1, 3, 5]
# 留空 [] 则绘制所有曲线 - Leave blank [] to plot all lines
plotting_lines = []

# x轴范围 - x-axis range
# 如需绘制0到100的x轴,填入[0, 100] - If you want to plot the x-axis from 0 to 100, fill in [0, 100]
# 留空则自动调整 - Leave blank for automatic adjustment
x_lim = []

# y轴范围 - y-axis range
# 如需绘制0到100的y轴,填入[0, 100] - If you want to plot the y-axis from 0 to 100, fill in [0, 100]
# 留空则自动调整 - Leave blank for automatic adjustment
y_lim = []


# -------------------以下内容不需要修改 Below this line, no need to modify-------------------

import matplotlib.pyplot as plt

x = []
y = []
settings = {'title': '', 'xaxis': '', 'yaxis': '', 'legend': False}
legends = []

with open(file_address, 'r') as file:
lines = file.readlines()
for line in lines:
if line.startswith('#'):
pass
elif line.startswith('@'):
param = line.split()
try:
if param[1] == 'title':
settings['title'] = line.split('"')[-2]
elif param[1] == 'xaxis':
settings['xaxis'] = line.split('"')[-2]
elif param[1] == 'yaxis':
settings['yaxis'] = line.split('"')[-2]
elif param[1] == 'TYPE':
if param[2] == 'xy':
pass
else:
print('This script only supports xy type xvg file')
exit()
elif param[1] == 'subtitle':
settings['title'] += '\n' + line.split('"')[-2]
elif param[1] == 'legend':
if param[2] == 'on':
settings['legend'] = True
elif param[2] == 'legend':
legends.append(line.split('"')[-2])
except IndexError:
pass

else:
try:
data = line.strip().split()
x.append(float(data[0]))

data_len = len(data) - 1
for i in range(data_len):
try:
y[i].append(float(data[i+1]))
except IndexError:
y.append([float(data[i+1])])
except (IndexError, ValueError):
pass

legends_new = []
for legend in legends:
legends_new.append(legend.replace('\\s', '$_{').replace('\\v{}\\z{}', '}$'))

if plotting_lines == []:
for i in range(len(y)):
if settings['legend']:
try:
plt.plot(x, y[i], label=legends_new[i])
except IndexError:
plt.plot(x, y[i])
else:
plt.plot(x, y[i])
else:
for i in plotting_lines:
if settings['legend']:
try:
plt.plot(x, y[i-1], label=legends_new[i-1])
except IndexError:
plt.plot(x, y[i-1])
else:
plt.plot(x, y[i-1])

if x_lim != []:
plt.xlim(x_lim)

if y_lim != []:
plt.ylim(y_lim)

plt.title(settings['title'])
plt.xlabel(settings['xaxis'])
plt.ylabel(settings['yaxis'])
plt.legend()

plt.savefig(file_address.replace('.xvg', '.png'))
plt.show()