如何在Matplotlib中的f字符串中显示LaTeX括号

问题描述

使用f字符串创建标签时,无法在Matplotlib图形中显示LaTeX括号。例如

fig,ax = plt.subplots(1,3,figsize=(12,4),sharey=True)
fig.suptitle("$x_n = x_{n-1}^2$," + f"$x_0={5:.2f},\,r={6:.2f},n \in {{ 0,\ldots,{7} }}$")

产生

enter image description here

如何在Matplotlib的f字符串中显示LaTeX括号?

解决方法

让我们看看您的f弦的价值是什么

>>> f"$x_0={5:.2f},\,r={6:.2f},n \in {{ 0,\ldots,{7} }}$"
'$x_0=5.00,\\,r=6.00,n \\in { 0,\\ldots,7 }$'
>>> 

哦,很好,但这已传递给LaTeX!在LaTeX中,括号是界定一组字符的活动字符,要在格式方程式中包含LITERAL括号,则需要引用括号

>>> f"$x_0={5:.2f},n \in \{{ 0,{7} \}}$"
'$x_0=5.00,n \\in \\{ 0,7 \\}$'
>>> 

重新更改字体,在您触摸注释时,您必须更改MATH字体,请参阅this answer

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...