如何使用 Latex 和 f-String 格式在图中注释数学方程

问题描述

这是代码片段:对曲线数学方程的注释。

# equation annotation 
plt.annotate(s =f"$exp({poly_regr.intercept_[0] : 0.2f} - {-poly_regr.coef_[0,0] : 0.2f}x + {poly_regr.coef_[0,1]: 0.4f}x^2)$",xy = (1975,0.4*(10**13)),color = "black",fontsize = 13,fontweight = "normal"
             )

enter image description here

更新 f-string 代码以使用漂亮的数学方程获得所需注释的可能方法是什么。 甚至如何编写更复杂的东西,例如:

enter image description here

解决方法

您可以使用 LaTeX 语法来完成。例如,将 plt.annotate 中的字符串替换为

fr"$e^{{({poly_regr.intercept_[0] : 0.2f} - {-poly_regr.coef_[0,0] : 0.2f}x + {poly_regr.coef_[0,1]: 0.4f}x^2)}}$"

或与

r"$e^{(\alpha + \beta + 48x)}$"