matplotlib mathtext不起作用

matplotlib mathtext不起作用,math,matplotlib,Math,Matplotlib,我正在做一个历史图,我想在图中显示一些数字,所以我用mathtext作为文本放在一个文本框中,但我不工作,我不明白为什么 a = [2086., 360.5, 1000.] b = [977., 37., 498.] c = [4512., 690., 378.] textstr = r'$\per50=%.2f$\n$\per16=%.2f$\n$\per84=%.2f$'%(a[0],b[0],c[0]) # these are matplotlib.patch.Patch

我正在做一个历史图,我想在图中显示一些数字,所以我用mathtext作为文本放在一个文本框中,但我不工作,我不明白为什么

a = [2086., 360.5, 1000.]

b = [977., 37., 498.]

c = [4512., 690., 378.]


textstr = r'$\per50=%.2f$\n$\per16=%.2f$\n$\per84=%.2f$'%(a[0],b[0],c[0])

    # these are matplotlib.patch.Patch properties
    props = dict(boxstyle='round', facecolor='wheat', alpha=0.75)

    # place a text box in upper left in axes coords
    ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
            verticalalignment='top', bbox=props)
在我的图的末尾,我得到了这个错误:

matplotlib.pyparsing.ParseFatalException: Expected end of math '$'
$\per50=2086.00$\n$\per16=977.00$\n$\per84=4512.00$ (at char 0), (line:1, col:1)

我希望你能帮助我

您收到该错误是因为命令
$\per$
不存在。这是您定义的latex命令吗?如果设置matplotlib参数text.usetex=True,则可以设置latex前导并在其中定义命令,例如:

rc('text', usetex=True)
rc('text.latex', preamble='something')

但我认为这不能用于定义新命令(不鼓励使用序言)。因此,最好是在matplotlib中显式编写
\per
所代表的内容。

应该显示的
\per
是什么?谢谢!我现在是这样做的:textstr='per50='+str(a[0])+'\nper16='+str(b[0])+'\nper84='+str(c[0]),这可能不是“完美”的方式,但它是有效的。但至少我现在知道了,错误是什么。Thanx很高兴这有帮助。顺便说一下,我在你的个人资料中看到你从未接受过答案。在这个网站上,这是一种给予认可的方式,奖励回答问题的人,同时也指导带着同样问题来到这里的人。所以你应该考虑接受更多的答案。