0

我有一个matplotlib图表,我想用 Latex Markdown 进行注释。

axs.annotate(s='$r_f = {:.2f}$'.format(r), xy=[2005, 7],
             xycoords="data", fontsize=18)

这很好用,但我想要下标中包含多个字母的乳胶代码。我需要$r_{fit}$,但这会混淆python格式方法的内部系统。

c:\Users\Dean\Dropbox\Dean\Study\_Thesis\2. Paper\code\paper\charts.py in plot_prediction(x, y, data, future_x, future_y, future_data, death)
    141         axs.scatter(x, data, label='Train Data', color=_palette[0])
    142         r, p = pearsonr(y, data)
--> 143         axs.annotate(s=r'$r_{fit} = {:.2f}$'.format(r), xy=[2005, 7],
    144                      xycoords="data", fontsize=18)
    145         # print p

KeyError: 'fit'

我尝试了许多带有转义字符的选项,但没有任何效果。

4

1 回答 1

1

问题在于格式/字符串而不是matplotlib,我得到了错误,

"$r_{fit} = {:.2f}$".format(10.)

 KeyError                                  Traceback (most recent call      last)
<ipython-input-41-2d336f9a0477> in <module>()
----> 1 "$r_{fit} = {:.2f}$".format(10.)

KeyError: 'fit'

这个问题在这个答案中得到了解决,他们使用双括号,

"$r_{{fit}} = {:.2f}$".format(r)

另一个简单的解决方法是拆分下标和格式语句,

"$r_{fit}" + " = {:.2f}$".format(r)

这也可以按预期工作。

于 2016-02-09T11:48:14.790 回答