<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
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.<br>
<br>
<pre class="lang-py prettyprint prettyprinted" style=""><code><span class="com"># -*- coding: utf-8 -*-</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> numpy </span><span class="kwd">as</span><span class="pln"> np
</span><span class="kwd">import</span><span class="pln"> matplotlib
matplotlib</span><span class="pun">.</span><span class="pln">use</span><span class="pun">(</span><span class="str">"WXAgg"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> matplotlib</span><span class="pun">.</span><span class="pln">pyplot </span><span class="kwd">as</span><span class="pln"> plt
plt</span><span class="pun">.</span><span class="pln">rc</span><span class="pun">(</span><span class="str">'text'</span><span class="pun">,</span><span class="pln"> usetex</span><span class="pun">=</span><span class="kwd">True</span><span class="pun">)</span><span class="pln">
plt</span><span class="pun">.</span><span class="pln">rc</span><span class="pun">(</span><span class="str">'text.latex'</span><span class="pun">,</span><span class="pln"> preamble </span><span class="pun">=</span><span class="pln"> r</span><span class="str">'\usepackage{xcolor}'</span><span class="pun">)</span><span class="pln">

N </span><span class="pun">=</span><span class="pln"> </span><span class="lit">50</span><span class="pln">
x </span><span class="pun">=</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">rand</span><span class="pun">(</span><span class="pln">N</span><span class="pun">)</span><span class="pln">
y </span><span class="pun">=</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">random</span><span class="pun">.</span><span class="pln">rand</span><span class="pun">(</span><span class="pln">N</span><span class="pun">)</span><span class="pln">

fig</span><span class="pun">,</span><span class="pln"> ax </span><span class="pun">=</span><span class="pln"> plt</span><span class="pun">.</span><span class="pln">subplots</span><span class="pun">()</span><span class="pln">
ax</span><span class="pun">.</span><span class="pln">scatter</span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> np</span><span class="pun">.</span><span class="pln">cos</span><span class="pun">(</span><span class="pln">x</span><span class="pun">),</span><span class="pln"> color </span><span class="pun">=</span><span class="pln"> </span><span class="str">'blue'</span><span class="pun">,</span><span class="pln"> marker </span><span class="pun">=</span><span class="pln"> </span><span class="str">"+"</span><span class="pun">,</span><span class="pln"> s</span><span class="pun">=</span><span class="lit">47.5</span><span class="pun">)</span><span class="pln">
ax</span><span class="pun">.</span><span class="pln">set_title</span><span class="pun">(</span><span class="pln">r</span><span class="str">'$\color{red}{X}X$'</span><span class="pun">)</span><span class="pln"> 
</span><span class="com">#ax.set_title(r"\textcolor{red}{X} $\color{red}{X}$") # does not work either</span><span class="pln">

plt</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()

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

--V
</span></code></pre>
<!DOCTYPE html>
<title>Page Title</title>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
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/
<br>
<br>
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
</body>
</html>