Home > Blockchain >  Superscripting a string in python
Superscripting a string in python

Time:04-06

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!

CodePudding user response:

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$ ')

CodePudding user response:

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)
  • Related