I am trying to create a plot with a legend containing a reaction where I need to use superscripts inside a loop. My variable ho_isots = [159,160,161,162,163,164]
and I would like to have the trends labeled as follow: natDy(p,x)161Ho, for example, instead I get natDy(p,x)161Ho.
Any suggestion? Thank you in advance.
This is the loop:
fig, ax = plt.subplots()
for isot,th_cs in zip(ho_isots,th_cs_holmium):
ax.plot(ep_thcs,th_cs,linewidth=2,label=f'$^{{nat}}$Dy(p,x)$^{isot}$Ho')
According to the PEP,
Expressions appear within curly braces '{' and '}'. While scanning the string for expressions, any doubled braces '{{' or '}}' inside literal portions of an f-string are replaced by the corresponding single brace.
So, your f-string label after processing looks like '$^{nat}$Dy(p,x)$^159$Ho'
(notice how double braces are replaced by a single one, and {isot}
got replaced with the actual value). This string gets indeed translated by Matplotlib as natDy(p,x)161Ho. To overcome this, you have to put the isot
value in the curly braces, making it triple: f'$^{{nat}}$Dy(p,x)$^{{{isot}}}$Ho'