'Superscripting a string in python

Hello I am trying to superscript a string for a plot.

hallo= str(round(popt[1],1))
plt.plot(xFit, func(xFit,*popt),color='r', linestyle='--',label=f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm F(x) = {round(popt[0],1)} * e$^{hallo}*x$ ')

And the result i get is:

enter image description here

The "-0,2*x" should be superscripted. What am I doing wrong? Thank you!



Solution 1:[1]

It IS making the - a superscript. To get the whole expression in there, you need to enclose the whole expression in curly braces. Since you have this in an f-string, where curly braces have meaning, that means you need these rather awkward triple-braces

plt.plot(xFit, func(xFit,*popt),color='r', linestyle='--',label=f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm F(x) = {round(popt[0],1)} * e$^{{{hallo}}}*x$ ')

Solution 2:[2]

I believe you need to wrap the whole expression with the $$, can't start with e$^10$, rather $e^10$

hallo = str(round(popt[1],1))
label = (f'Ideales DOE 125 \u03bcJ <= 0,3 \u03bcm '
         f'F(x) = {round(popt[0],1)} * $e^{hallo}*x$ ')
plt.plot(xFit, func(xFit,*popt),color='r', linestyle='--',label=label)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts
Solution 2 dasfacc