Skip to main content
 首页 » 编程设计

python之在 matplotlib 的注释中格式化数学文本

2024年10月24日13三少

我正在尝试用一些math 之类的文本在我的情节中写一段文本

在得到很多类似的错误之后

>>> 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 

但仍然无法得到我想要的结果

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

有什么建议吗?

请您参考如下方法:

你的第二个解决方案的格式化部分对我来说很好,但它可能会让你的生活更容易通过将数字放在双大括号中来转义格式化:

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

例如xc = 5,输出

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