乳胶在matplotlib中无法正确显示

时间:2016-07-07 07:33:55

标签: python matplotlib latex jupyter

使用jupyter / ipython在python中执行以下代码时,Latex符号无法正确显示。任何想法为什么会这样?

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = np.linspace(0,3)
y = np.sin(x)
plt.plot(x,y)
plt.title(r'$\beta \rho \lambda \xi$',fontsize=30)

输出:

enter image description here

1 个答案:

答案 0 :(得分:2)

IPython笔记本使用MathJax在html / markdown中呈现LaTeX。把你的LaTeX数学放在$$。

$$c = \sqrt{a^2 + b^2}$$

enter image description here

你必须写:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,3)
y = np.sin(x)
plt.plot(x,y)
plt.title(r'$$\beta \rho \lambda \xi$$',fontsize=30)

plt.show()

--------------------

或者您可以显示Python的LaTeX / Math输出,如notebook tour末尾所示:

from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

enter image description here

---------------------

其他解决方案:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}'] #for \text command

x = np.linspace(0,3)
y = np.sin(x)
plt.plot(x,y)
plt.title(r'$text{\beta \rho \lambda \xi}$',fontsize=30)

plt.show()