我想对x和y给出的数据做线性回归。当我使用线性图时,一切似乎都很好,但是当我想在对数尺度上绘制时,线看起来并不直。我想我应该把区间分成更细的网格,而不是只分成六个点。但我做不到。
我如何在下面的脚本的对数刻度上进行线拟合?
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1560., 526., 408., 226., 448., 288.])
y = np.array([0.118, 0.124, 0.131, 0.160, 0.129, 0.138])
f = np.multiply(x,y**2)
coefs = np.polyfit(x, f, 1)
pred_f = coefs[1] + np.multiply(sorted(x), coefs[0])
fig, ax1 = plt.subplots(1, 1, figsize=(8,6))
ax1.scatter(x, f)
ax1.plot(sorted(x), pred_f, 'k--')
ax1.set_xscale('log')
ax1.set_yscale('log')
plt.show()
1条答案
按热度按时间n6lpvg4x1#
事实上,“直线”(线性函数)在双对数图上看起来并不直:
测试结果:
为了适应对数尺度,对原始数据的对数进行回归:
剧情:
或者让Matplotlib绘制log ticks。然后,您需要对
pred_f
取幂,使其与数据处于相同的标度:图是相同的,但它现在使用数据的原始比例: