用python渲染乳胶文本 [英] Render Latex text with python
本文介绍了用python渲染乳胶文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用python渲染Latex文本.这就是我试图做的:
将matplotlib.pyplot导入为plttxte = r"""\emph{特征多项式} $\chi(\lambda)$$3 \times 3$~矩阵\ [\ left(\ begin {array} {ccc}&b&C \\d&电子&F \\g&和我 \end{array} \right)\]由公式给出\[ \chi(\lambda) = \left|\ begin {array} {ccc}\ lambda-一个&-b&-C \\-d &\ lambda-e&-F \\-g&-h &\lambda - i \end{array} \right|.\]"""plt.text(0.0,0.0,txte,fontsize = 10)无花果= plt.gca()fig.axes.get_xaxis().set_visible(False)fig.axes.get_yaxis().set_visible(False)plt.draw() #或 savefigplt.show()
正确渲染后,它应该输出:
然而,这是我得到的:
有什么想法吗?
谢谢!
解决方案
您必须将以下行添加到代码中,以通过您自己安装的软件来呈现乳胶文本(默认情况下,matplotlib使用MathText:
I'm trying to render Latex text with python. This is what I tried to do:
import matplotlib.pyplot as plt
txte = r"""
The \emph{characteristic polynomial} $\chi(\lambda)$ of the
$3 \times 3$~matrix
\[ \left( \begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i \end{array} \right)\]
is given by the formula
\[ \chi(\lambda) = \left| \begin{array}{ccc}
\lambda - a & -b & -c \\
-d & \lambda - e & -f \\
-g & -h & \lambda - i \end{array} \right|.\]
"""
plt.text(0.0,0.0, txte,fontsize=10)
fig = plt.gca()
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.draw() #or savefig
plt.show()
When rendered correctly, it should output:
However, this is what I get:
Any ideas?
Thanks!
解决方案
You have to add to your code these lines to render latex text by your own installed software (by default matplotlib use MathText: http://matplotlib.org/api/mathtext_api.html):
from matplotlib import rcParams
rcParams['text.usetex'] = True
The second problem is that you have to put your latex string to one line (and you forget $-brackets for matrices):
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
txte = r"The \emph{characteristic polynomial} $\chi(\lambda)$ of the $3 \times 3$~matrix \\ $\left( \begin{array}{ccc} a & b & c \\ d & e & f \\g & h & i \end{array} \right) $ \\is given by the formula\\ $ \chi(\lambda) = \left| \begin{array}{ccc} \lambda - a & -b & -c \\ -d & \lambda - e & -f \\ -g & -h & \lambda - i \end{array} \right|. $"
plt.text(0.0, 0.0, txte, fontsize=14)
ax = plt.gca()
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
plt.show()
这篇关于用python渲染乳胶文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文