LaTeX在matplotlib图的标题中无法正确呈现

时间:2014-11-28 17:20:25

标签: matplotlib latex

我有以下代码,但标题无法正确呈现。

#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np

t = np.array([0.19641715476064042,
 0.2524989689506581,
 0.3411612727146944,
 0.4232070334074150])

c = np.array([0.17999670292553419,
 0.21057542001074211,
 0.2752899228849294,
 0.36577079519040556])


plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.title('$P(A_i = 0| A_j = 0 \forall j < i)$')

plt.plot(t, '-go', markerfacecolor='w', linestyle= 'dotted', label='n=20')
plt.plot(c, '-bo',  markerfacecolor='w', linestyle= 'dotted', label='n=22')
plt.legend()
plt.show()

这是我从渲染图像中看到的图像。我应该做什么呢?

enter image description here

2 个答案:

答案 0 :(得分:4)

您可以使用原始字符串来阻止\f具有特殊含义:

plt.title(r'$P(A_i = 0| A_j = 0 \forall j < i)$')

请注意,字符串以r开头,这使其成为原始字符串。

答案 1 :(得分:2)

你需要逃避&#34; \ f&#34;

 plt.title('$P(A_i = 0| A_j = 0 \\forall j < i)$')

见双&#34; \&#34;?这会导致&#34; \ f&#34;不应被解释为换页。