The reason why the annotation is not displayed in the generated plot is because the plt.text() function is called after the plt.show() function. In order to display the annotation in the plot, you need to call the plt.text() function before calling plt.show(). Here's the modified code:

# ...

ZZ = results.params[0]+data.lnk*data.lnk*results.params[1]+data.lnk*results.params[2]+data.lnl*results.params[3]+data.lnk*data.lnl*results.params[4]+data.lnl*data.lnl*results.params[5]
R2 = calculate_r_squared(data.lny,ZZ )

fig = plt.figure()
plt.xlabel('Year')
plt.ylabel('Output logarithm')
plt.plot(data.year,ZZ,label='Fitted value')
plt.plot(data.year,data.lny,label='Actual value')
plt.legend()

plt.text(4, 5, r'$R^2 = $' + str(R2), fontsize=12, color='red')
plt.show()

By placing the plt.text() function before the plt.show() function, the annotation will be displayed in the generated plot.


原文地址: https://www.cveoy.top/t/topic/phtS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录