Latex颜色在matplotlib文本中未渲染

4

我的绘图脚本中好像无法使用Tex的\textcolor命令。

import matplotlib as matplotlib
from matplotlib import pyplot as plt

matplotlib.rcParams.update({'text.usetex': True})

matplotlib.rc(
    'text.latex', preamble=r"\usepackage{xcolor}")

fig, ax = plt.subplots()
ax.set_ylabel(r'\textcolor{red}{aaaaaaa}')
plt.show()

它没有给我红色文本,它生成了:

enter image description here

我有什么遗漏吗?


请查看此链接:https://dev59.com/_2ox5IYBdhLWcg3wf0Xl - Sheldore
原则上,matplotlib 无法对文本的不同部分进行着色;可以在此示例中找到解决方法。 - ImportanceOfBeingErnest
1个回答

1
这里有更详细的解释:https://matplotlib.org/users/usetex.html。但似乎只有在导出为ps文件时才有效。对我来说,如果将其保存为ps文件,则以彩色形式工作,而同一文件的内联方式则无法工作。
这里有一个小的解决方法。
import matplotlib as matplotlib
from matplotlib import pyplot as plt

matplotlib.rcParams.update({'text.usetex': True})
matplotlib.rc('text.latex', preamble=r"\usepackage{xcolor}")

fig, ax = plt.subplots()
ax.set_ylabel(r"aaaaaaa", color='r')

#plt.savefig(r"foo.ps")
# you can include the above line if you're using your old code.

plt.show()

我正在尝试让它在更复杂的代码中运行,其中包括一个我保存为gif的动画。所以很不幸,将其保存为ps并不是一个真正的选择。 - P. Camilleri
是的,很多人都遇到了这个问题。使用上述方法是否可以解决它(不涉及保存到 PS,即使用 color='r')?还是代码涉及其他内容? - rpm787
我正在使用这个来着色文本的一部分,所以我认为我不能。 - P. Camilleri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接