[Matplotlib-users] Unable to render different colors for specific characters within a LaTeX string

Virgil Stokes vs at it.uu.se
Mon Feb 4 14:19:20 EST 2019


I would like to change the color of a substring within a LaTeX string for a MatPlotLib plot title. The code below gives no errors or warning; 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()

How can I render the first X with the color red?

--V









När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: http://www.uu.se/om-uu/dataskydd-personuppgifter/

E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: http://www.uu.se/en/about-uu/data-protection-policy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190204/d323e761/attachment.html>


More information about the Matplotlib-users mailing list