Unable to render different colors for specific characters within a LaTeX string
I need to change the color of substrings within a LaTeX string for a plot title. I have tried several different approaches, most of which gave errors and/or warnings. The code below gives no errors or warnings, but does not render the color specified.
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
matplotlib.use("WXAgg")
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble = r'\usepackage{xcolor}')
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
fig, ax = plt.subplots()
ax.scatter(x, np.cos(x), color = 'blue', marker = "+", s=47.5)
ax.set_title(r'$\color{red}{X}X$')
#ax.set_title(r"\textcolor{red}{X} $\color{red}{X}$") # does not work either
plt.show()