本文介绍了对数对数图的多项式拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的问题来在对数对数刻度上拟合一条直线.我的代码是,
I have a simple problem to fit a straight line on log-log scale. My code is,
data=loadtxt(filename)
xdata=data[:,0]
ydata=data[:,1]
polycoeffs = scipy.polyfit(xdata, ydata, 1)
yfit = scipy.polyval(polycoeffs, xdata)
pylab.plot(xdata, ydata, 'k.')
pylab.plot(xdata, yfit, 'r-')
现在我需要在对数刻度上绘制拟合线,所以我只需更改 x 和 y 轴,
Now I need to plot fit line on log scale so I just change x and y axis,
ax.set_yscale('log')
ax.set_xscale('log')
然后它没有绘制正确的拟合线.那么如何更改拟合函数(以对数刻度),以便它可以在对数刻度上绘制拟合线?
then its not plotting correct fit line. So how can I change fit function (in log scale) so that it can plot fit line on log-log scale?
推荐答案
from scipy import polyfit
data = loadtxt("data.txt")
xdata,ydata = data[:,0],data[:,1]
xdata,ydata = zip(*sorted(zip(xdata,ydata))) # sorts the two lists after the xdata
xd,yd = log10(xdata),log10(ydata)
polycoef = polyfit(xd, yd, 1)
yfit = 10**( polycoef[0]*xd+polycoef[1] )
plt.subplot(211)
plt.plot(xdata,ydata,'.k',xdata,yfit,'-r')
plt.subplot(212)
plt.loglog(xdata,ydata,'.k',xdata,yfit,'-r')
plt.show()
这篇关于对数对数图的多项式拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!