pythonstringmatplotlibannotationslatex

How to annotate maths equation in graph using both latex and f-String formatting


Here's the code snippet : annotation of mathematical equation of the cuve.

# 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

what's the possible way to update the f-string code to get the desired anotation with a beautiful mathematical equations. or even how to write more complex things , for example :enter image description here


Solution

  • You can do it using LaTeX syntax. For example, replace the string in plt.annotate with

    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)}}$"
    

    or with

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