The Setup
I have a pandas
dataframe that contains a column 'iso' containing chemical isotope symbols, such as '4He', '16O', '197Au'. I want to label many (but not all) isotopes on a plot using the annotate()
function in matplotlib
. The label format should have the atomic mass in superscript. I can do this with the LaTeX style formatting:
axis.annotate('$^{4}$He', xy=(x, y), xycoords='data')
I could write dozens of annotate()
statements like the one above for each isotope I want to label, but I'd rather automate.
The Question
How can I extract the isotope number and name from my iso column?
With those pieces extracted I can make the labels. Lets say we dump them into the variables Num
and Sym
. Now I can loop over my isotopes and do something like this:
for i in list_of_isotopes:
(Num, Sym) = df[df.iso==i].iso.str.MISSING_STRING_METHOD(???)
axis.annotate('$^{%s}$%s' %(Num, Sym), xy=(x[Num], y[Num]), xycoords='data')
Presumably, there is a pandas
string methods that I can drop into the above. But I'm having trouble coming up with a solution. I've been trying split()
and extract()
with a few different patterns, but can't get the desired effect.