我将matplotlib与latex标签一起用于轴、标题和颜色栏标签
虽然它在大多数情况下工作得很好,但当你有一个使用\text的公式时,它会有一些问题。
一个非常简单的例子。
from matplotlib import pyplot as plt
plt.plot([1,2,3])
plt.title(r"$f_{\text{cor, r}}$")
plt.show()
这将导致如下错误消息:
IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter:
f_{\text{1cor, r}}
^
Unknown symbol: \text (at char 3), (line:1, col:4)
FormatterWarning,
有没有一种简单的方法来使用\text?
发布于 2014-05-23 18:06:41
一种选择是让matplotlib为您的文本渲染提供use LaTeX directly (而不是matplotlib提供的mathtext实现)。
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
# (create your plot as before)
plt.title(r"$f_{\mathrm{cor, r}}$")
这需要完全正常工作的LaTeX安装。我似乎不能让它识别\text
,但\mathrm
确实工作得很好(对于‘罗马家族’字体)。
https://stackoverflow.com/questions/23824687
复制相似问题