Dalek
Dalek

Reputation: 4318

formatting math text in annotate of matplotlib

I am trying to write a text in my plot with some math text like

enter image description here

After getting a lot of errors like

>>> plt.annotate(r'$\log_{10}M_{200}={:.2f}$'.format(xc), xy=(0.9, 0.95), xycoords='axes fraction', size=18, color='blue')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

IndexError: tuple index out of range

But still with the following line I could not get what I want

import pylab as plt
xc=0.24
plt.annotate(r'$\log_{:.2}M_{:.3}={:.2f}$'.format('10','200',xc), xy=(0.8, 0.95), xycoords='axes fraction', size=15, color='blue') 

any suggestion?

Upvotes: 2

Views: 2074

Answers (1)

clwainwright
clwainwright

Reputation: 1704

The formatting part of your second solution works fine for me, but it might make your life a little easier to escape the formatting by putting numbers in double curly brackets:

r'$\log_{{10}}M_{{200}}={:.2f}$'.format(xc)

For e.g. xc = 5, this outputs

$\log_{10}M_{200}=5.00$

Upvotes: 2

Related Questions