简体   繁体   中英

formatting math text in annotate of matplotlib

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

在此处输入图片说明

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?

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 eg xc = 5 , this outputs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM