<p>这个问题的推理部分正确。有一个函数<code>f(x) = a*x +b</code>,您可以将y轴(x=0)为<code>(0, b)</code>(在本例中为<code>(0,-0.8188)</code>)的截取作为第一个点
该行上的任何其他点由<code>(x, f(x))</code>,或<code>(x, a*x+b)</code>给出。所以看x=100处的点会得到<code>(100, f(100))</code>,插入:<code>(100, 0.1462*100-0.8188)</code>=<code>(100,13.8012)</code>。
如果你在问题中描述你只是忘了考虑<code>b</code>。</p>
<p>下面显示如何使用该函数在matplotlib中绘制直线:</p>
<pre><code>import matplotlib.pyplot as plt
import numpy as np
bill = [34,108,64,88,99,51]
tip = [5,17,11,8,14,5]
plt.scatter(bill, tip)
#fit function
f = lambda x: 0.1462*x - 0.8188
# x values of line to plot
x = np.array([0,100])
# plot fit
plt.plot(x,f(x),lw=2.5, c="k",label="fit line between 0 and 100")
#better take min and max of x values
x = np.array([min(bill),max(bill)])
plt.plot(x,f(x), c="orange", label="fit line between min and max")
plt.legend()
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/ZRBjj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZRBjj.png" alt="enter image description here"/></a></p>
<p>当然,装配也可以自动完成。您可以通过调用<code>numpy.polyfit</code>获得斜率和截距:</p>
<pre><code>#fit function
a, b = np.polyfit(np.array(bill), np.array(tip), deg=1)
f = lambda x: a*x + b
</code></pre>
<p>其余的情节将保持不变。</p>