Partial coloring of text in matplotlib

I only know how to do this non-interactively, and even then only with the ‘PS’ backend.

To do this, I would use Latex to format the text. Then I would include the ‘color’ package, and set your colors as you wish.

Here is an example of doing this:

import matplotlib
matplotlib.use('ps')
from matplotlib import rc

rc('text',usetex=True)
rc('text.latex', preamble="\usepackage{color}")
import matplotlib.pyplot as plt

plt.figure()
plt.ylabel(r'\textcolor{red}{Today} '+
           r'\textcolor{green}{is} '+
           r'\textcolor{blue}{cloudy.}')
plt.savefig('test.ps')

This results in (converted from ps to png using ImageMagick, so I could post it here):
enter image description here

Leave a Comment