简体   繁体   中英

Latex colors not rendered in matplotlib text

Tex's \\textcolor seems to be ignored in my plottling script

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

does not give me a red text, it produces:

在此处输入图片说明

Am I missing something?

It's explained in more detail here : https://matplotlib.org/users/usetex.html but seems like it only works when you export it to a ps file. For me, it works in color if you're saving it as a ps file while the same file inline doesn't work.

Slight workaround here.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM