【发布时间】:2018-10-09 10:39:27
【问题描述】:
我使用 matplotlib 绘制了四个单独的点,并希望通过它们找到最佳拟合线的斜率。或者,我想将这些点绘制为一条线,并尽可能找到斜率。我也试图在对数尺度上做到这一点。这是我所拥有的(期间计算来自我代码中的其他地方):
import matplotlib.pyplot as plt
# First orbit
x_0 = 10.0
a1 = x_0
T1 = len(range(1, 98))
# Second orbit
x_0 = 5.0
a2 = x_0
T2 = len(range(1, 63))
# Third orbit
x_0 = 7.0
a3 = x_0
T3 = len(range(1, 81))
# Fourth orbit
x_0 = 13.0
a4 = x_0
T4 = len(range(1, 138))
smaxis = [a1, a2, a3, a4]
T = [T1, T2, T3, T4]
# Plot period versus semi-major axis
for i in range(len(T)):
plt.plot(T[i], smaxis[i], markersize=3, marker='o')
plt.xlabel('Period (T)')
plt.ylabel('Semimajor Axis (a)')
plt.xscale('log')
plt.yscale('log')
plt.title('Logarithmic scale of T vs a')
我已经尝试在这段代码中使用 linregress:
from scipy.stats import linregress
linregress(T, smaxis)
但我不相信这是正确的,因为 T 和 smaxis 是列表,我需要通过显示的离散点的最佳拟合线之间的斜率。我该怎么做?
【问题讨论】:
标签: python python-2.7 matplotlib scipy linear-regression