首页 > 解决方案 > 乳胶颜色未在 matplotlib 文本中呈现

问题描述

\textcolor在我的绘图脚本中似乎忽略了Tex

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()

没有给我一个红色的文字,它产生:

在此处输入图像描述

我错过了什么吗?

标签: pythonmatplotliblatex

解决方案


这里有更详细的解释: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()

推荐阅读