如何为U型势能散点图拟合贴合形状的边界曲线?
拟合U型势能曲线的实现方法
针对你的U型势能散点图,推荐两种实用的曲线拟合方案,直接基于你的现有代码修改即可:
方案1:二次多项式拟合(物理意义匹配U型势能)
U型分布最贴合二次多项式(对应简谐势这类经典U型势能模型),形式为 U(r) = a*r² + b*r + c,实现步骤如下:
import numpy as np import matplotlib.pyplot as plt # 你的原有代码 frequencies_first=trajec_first['r'].value_counts().sort_index() probabilities_first=frequencies_first/frequencies_first.sum() energies_first=-np.log(probabilities_first) energies_first0=energies_first.index-radious # 提取拟合用的x、y数据 x = energies_first0.values y = energies_first.values # 拟合二次多项式(degree=2指定二次) coeffs = np.polyfit(x, y, 2) fit_func = np.poly1d(coeffs) # 生成密集的x点让曲线更平滑 x_fit = np.linspace(min(x), max(x), 100) y_fit = fit_func(x_fit) # 绘制散点+拟合曲线 plt.plot(x, y, '^', label='5min') plt.plot(x_fit, y_fit, '-', color='crimson', label='U型拟合曲线') plt.xlabel('r (micron)') plt.ylabel('energia (kT)') plt.legend() plt.show()
方案2:平滑样条插值(贴合散点细节)
如果二次拟合不够匹配散点的细节波动,可以用样条插值生成更贴近原始数据的平滑曲线:
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import make_interp_spline # 你的原有代码 frequencies_first=trajec_first['r'].value_counts().sort_index() probabilities_first=frequencies_first/frequencies_first.sum() energies_first=-np.log(probabilities_first) energies_first0=energies_first.index-radious x = energies_first0.values y = energies_first.values # 创建平滑样条(k=3为三次样条,可调整为2贴近二次U型) spline = make_interp_spline(x, y, k=3) x_fit = np.linspace(min(x), max(x), 100) y_fit = spline(x_fit) # 绘制 plt.plot(x, y, '^', label='5min') plt.plot(x_fit, y_fit, '-', color='forestgreen', label='平滑拟合曲线') plt.xlabel('r (micron)') plt.ylabel('energia (kT)') plt.legend() plt.show()
补充说明
- 若你的U型存在不对称性,可尝试将多项式阶数调整为3(三次多项式),但二次多项式仍是U型势能的经典物理模型
- 样条插值的平滑程度可通过
k参数控制,k=2会更偏向二次U型的形态 - 你的代码已通过
sort_index()保证了x数据的有序性,这是拟合的前提条件
内容的提问来源于stack exchange,提问作者Gonzalo Sisniega
相关产品推荐
相关产品推荐

