matplotlib mathtext不起作用

时间:2012-12-21 09:59:06

标签: 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 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)

我希望你能帮助我!

1 个答案:

答案 0 :(得分:3)

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

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

但我认为这不能用于定义新命令(并且不鼓励使用前导码)。因此,最好的办法是在matplotlib中明确写出\per代表的内容。

相关问题