【问题标题】:Plotting a least squares best fit line using calculated values of y=ax+b使用 y=ax+b 的计算值绘制最小二乘最佳拟合线
【发布时间】:2016-08-29 20:42:17
【问题描述】:

我正在编写代码来查找导入文件中某些数据的最佳拟合线的最小二乘。线的方程是ax+b,我已经计算了ab。绘制我尝试过的线:

LS_fit_ydata = []
for i in x_data:
    y_new = ((i*b) + a)
    LS_fit_ydata.append(y_new)

我正在使用matplotlib.pyplot as plt 来绘制我的图表。

没有错误消息,但这条线没有出现在我的图表上。有谁知道出了什么问题?感谢您提供的任何帮助。

【问题讨论】:

  • 到目前为止,您共享的代码中没有绘图。它只是通过一个 for 循环,并将 y_new 附加到列表

标签: python matplotlib graph best-fit


【解决方案1】:

您缺少的是代码中的绘图部分:

# The code you provided
LS_fit_ydata = []
for i in x_data:
    y_new = ((i*b) + a)
    LS_fit_ydata.append(y_new)

# What happens here is you're plotting x against y one by one via the list
plt.plot(x_data, y_new)
# Show the graph
plt.show()

【讨论】:

猜你喜欢
  • 2014-05-05
  • 1970-01-01
  • 2012-08-29
  • 2014-03-05
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 2017-03-31
相关资源
最近更新 更多