想在Python中显示拟合公式吗

2024-04-18 12:10:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我用一个方程拟合数据,我想显示这个拟合方程。你知道吗

下面似乎没有显示yïu方程(其他的都可以)。思想?你知道吗

data = np.loadtxt("datafile.dat")
x= data[:,0]
y= data[:,1]
plt.scatter(x,y)


x_equation = np.linspace(0,10,100)
y_equation = np.sin(x_equation)      # of course this is actually very long and messy

plt.plot(x_equation,y_equation)
plt.text(y_equation)

Tags: of数据datanppltsindat思想
1条回答
网友
1楼 · 发布于 2024-04-18 12:10:05

我不认为您可以只使用变量y_equation作为文本。一种方法是将表达式显式地写为字符串。在这种情况下,需要指定用于定位字符串的x和y坐标,然后使用plt.text指定字符串,如下所示。如果你还需要别的东西,请告诉我。你知道吗

x_equation = np.linspace(0,10,100)
y_equation = np.sin(x_equation)      

plt.plot(x_equation,y_equation)
plt.text(4, 0.75, 'sin(x)', fontsize=20)

enter image description here

相关问题 更多 >