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

2024-04-26 01:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的绘图中用一些math文本来写一个文本

enter image description here

在遇到很多错误之后

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

但我还是不能得到我想要的

^{pr2}$

有什么建议吗?在


Tags: 文本logformat绘图size错误pltmath
1条回答
网友
1楼 · 发布于 2024-04-26 01:31:33

第二个解决方案的格式化部分对我来说很好,但如果将数字放在两个大括号中,可能会让您的生活更轻松一些:

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

对于例如xc = 5,此输出

^{pr2}$

相关问题 更多 >