如何在matplotlib坐标轴中写latex特殊字符$\bar{T}$?

5 投票
1 回答
8883 浏览
提问于 2025-04-18 16:55

我想在用matplotlib绘制的图表的坐标轴标签中写入latex数学模式符号 $\bar{T}$。文档中提到数学模式不被支持,所以我尝试了

plt.xlabel(r'$\displaystyle \={T}$',fontsize=12)

plt.xlabel(r'$\={T}$',fontsize=12)

但出现了错误

matplotlib.pyparsing.ParseFatalException: Expected end of math '$'
$\displaystyle \={T}$ (at char 0), (line:1, col:1)

    raise ParseFatalException(msg + "\n" + s)
matplotlib.pyparsing.ParseFatalException: Expected end of math '$'
$\={T}$ (at char 0), (line:1, col:1)
>>> 

有没有办法在matplotlib的坐标轴标签中写这个符号呢?我能写其他的latex坐标轴标签,但我从来没有用过这些 特殊字符。

1 个回答

4

你链接的文档页面是在讲如何调用Latex来提供格式化的文本,但其实matplotlib自己有一个内置的数学表达式解析器,可以很好地处理大部分Latex的数学命令,而不需要真正去运行外部的Latex命令。除非你特别设置了你的matplotlib使用外部的Latex安装,否则你还是在使用内置的数学解析器,它可以很好地处理\bar{}这个命令:

plt.plot(range(5), range(5))
plt.xlabel(r'$\bar{T}$',fontsize=12)
plt.show()

撰写回答