我有一個檔案(file.xvg),我使用 matplotlib 和 numpy (版本 Python 3.9.12)繪制結果。這是我的腳本:
import matplotlib.pyplot as plt
import numpy
import numpy as np
from scipy import signal
x, y = numpy.loadtxt("file.xvg", unpack=True)
fig = plt.figure(figsize=(13,8))
ax = fig.add_subplot(111)
ax.plot(x, y, color="k", linestyle='solid', linewidth=0.8)
ax.set_xlabel("Times (ps)", fontweight='bold', fontsize = 18, labelpad=3)
ax.set_ylabel("Pressures (bar)", fontweight='bold', fontsize = 18, labelpad=3)
plt.show()
和檔案.xvg
0.0000000 0.0287198 0.0100000 0.0655187 0.0200000 0.0665948 0.0300000 0.0676697 0.0400000 0.0797021 0.0500000 0.0883750 0.0600000 0.0824649 0.0700000 0.0726798 0.0800000 0.0749663 0.0900000 0.0746549 0.1000000 0.0767466 0.1100000 0.1051620 0.1200000 0.0846607 0.1300000 0.0746683 0.1400000 0.0744862 0.1500000 0.0913541 0.1600000 0.0844304 0.1700000 0.0750595 0.1800000 0.0783450 0.1900000 0.0869718 0.2000000 0.0969575 0.2100000 0.0924280 0.2200000 0.0759971 0.2300000 0.0704025 . . .
我想繪制運行平均值,如下圖所示:

繪圖平均值為7.5 ± 160.5 bar
uj5u.com熱心網友回復:
您必須為您的和陣列計算您的運行平均值(選擇一個值視窗rang) :xy
x_avg = []
y_avg = []
rang = 10
for ind in range(len(y)-rang 1):
y_avg.append(np.mean(y[ind:ind rang]))
x_avg.append(np.mean(x[ind:ind rang]))
ax.plot(x_avg, r_avg, color="red", linestyle='solid', linewidth=0.8)
PS:真是個倒退,那是 GROMACS 嗎?:)
uj5u.com熱心網友回復:

import numpy as np
from pandas import DataFrame as df
import matplotlib.pyplot as plt
origin_data = np.array([
[0.0000000, 0.0287198],
[0.0100000, 0.0655187],
[0.0200000, 0.0665948],
[0.0300000, 0.0676697],
[0.0400000, 0.0797021],
[0.0500000, 0.0883750],
[0.0600000, 0.0824649],
[0.0700000, 0.0726798],
[0.0800000, 0.0749663],
[0.0900000, 0.0746549],
[0.1000000, 0.0767466],
[0.1100000, 0.1051620],
[0.1200000, 0.0846607],
[0.1300000, 0.0746683],
[0.1400000, 0.0744862],
[0.1500000, 0.0913541],
[0.1600000, 0.0844304],
[0.1700000, 0.0750595],
[0.1800000, 0.0783450],
[0.1900000, 0.0869718],
[0.2000000, 0.0969575],
[0.2100000, 0.0924280],
[0.2200000, 0.0759971],
[0.2300000, 0.0704025],
])
n = origin_data.shape[0]
data = df(origin_data, columns=['x', 'y'])
window = 6
roll_avg = data.rolling(window).mean()
roll_avg_cumulative = data['y'].cumsum()/np.arange(1, 25)
avg = data['y'].mean()
std_error = data['y'].std()
print('{:.2f} /- {:.2f}'.format(avg, std_error))
# all data
plt.plot(data['x'], data['y'], c='b')
# rolling avg by "window"
plt.plot(roll_avg['x'], roll_avg['y'], c='r')
# cumulative avg
plt.plot(data['x'], roll_avg_cumulative, c='orange')
# horizontal line at overall avg
plt.hlines(avg, data['x'].iloc[0], data['x'].iloc[-1],
ls='--',
colors=['k'])
# ymax - ymin band
plt.fill_between(data['x'], avg std_error, avg-std_error, alpha=0.5)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/519651.html
上一篇:如何確保熊貓日期范圍的間距均勻?
下一篇:numpy對角函式很慢
