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 """
file_address = r"xxx.xvg"
plotting_lines = []
x_lim = []
y_lim = []
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()
|