4

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:

enter image description here

Am I missing something?

P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
  • Check [this](https://stackoverflow.com/questions/9169052/partial-coloring-of-text-in-matplotlib) – Sheldore Mar 23 '19 at 10:49
  • In principle, matplotlib cannot colorize parts of a text differently; a workaround is shown in [this example](https://matplotlib.org/gallery/text_labels_and_annotations/rainbow_text.html). – ImportanceOfBeingErnest Mar 23 '19 at 10:54

1 Answers1

1

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()
rpm787
  • 81
  • 6
  • I'm trying to make it work in a more complicated code, involving an Animation that I save as gif. So saving to ps is not really I option unfortunately, I think. – P. Camilleri Mar 23 '19 at 04:13
  • Yeah, that was the problem a lot of people had. Is it possible to use the method above to do it(which doesn't involve saving to ps i.e. using color='r') , or does the code involved something else ? – rpm787 Mar 23 '19 at 04:16
  • I am using this to color only a portion of the text so I don't think I can – P. Camilleri Mar 23 '19 at 06:20