问题描述
使用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} }}$")
产生
如何在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。