简体   繁体   English

Matplotlib上的最佳拟合线

[英]Line of Best fit on Matplotlib

I'm trying to plot a line of best fit on my auto generated graph. 我正在尝试在自动生成的图形上绘制一条最合适的线。 My graph is currently just a plain scatter graph. 我的图目前只是一个普通的散点图。 I've looked at various solutions to this, however, they all provide a solution to a different method of plotting the graph. 我已经看过各种解决方案,但是,它们都为另一种绘制图形的方法提供了解决方案。

My code loops through lists in order to plot the data stored in them. 我的代码遍历列表,以便绘制存储在其中的数据。

I'm wondering if there is a way of implementing a line of best fit with this method or whether I'll have to change it; 我想知道是否有一种方法可以用这种方法实现最合适的生产线,或者我是否必须更改它? if an explanation could be given as to how to change it to work, that would be appreciated. 如果可以就如何更改使其工作给出解释,将不胜感激。

for x in range (0,len(profits)):
    plt.plot([yearofreleaselist[x]], [profits[x]], '-ro')
    plt.annotate((filmlist[x]), xy=(yearofreleaselist[x], profits[x]))
    oldestfilm = len(yearofreleaselist)
    oldestfilm = oldestfilm-1
    plt.axis([(int(yearofreleaselist[oldestfilm])-1), (int(yearofreleaselist[0]))+1, 0, (max(profits))+50000000])  
plt.ylabel("Profit ($)")
plt.xlabel("Year Of Release")
name = str(textbox1.get())
plt.title("The Profits of " + name.title() + "'s films")
plt.savefig(name+'.png')
text3["text"] = plt.show()

If you want a linear best fit, how about: 如果要线性最佳拟合,该如何做:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(yearofreleaselist, np.poly1d(np.polyfit(yearofreleaselist, profits, 1))(yearofreleaselist))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM