繁体   English   中英

Matplotlib上的最佳拟合线

[英]Line of Best fit on Matplotlib

我正在尝试在自动生成的图形上绘制一条最合适的线。 我的图目前只是一个普通的散点图。 我已经看过各种解决方案,但是,它们都为另一种绘制图形的方法提供了解决方案。

我的代码遍历列表,以便绘制存储在其中的数据。

我想知道是否有一种方法可以用这种方法实现最合适的生产线,或者我是否必须更改它? 如果可以就如何更改使其工作给出解释,将不胜感激。

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()

如果要线性最佳拟合,该如何做:

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