Partial coloring of text in matplotlib


Question

Is there a way in matplotlib to partially specify the color of a string?

Example:

plt.ylabel("Today is cloudy.")

How can I show "today" as red, "is" as green and "cloudy." as blue?

Thanks.

1
36
2/6/2012 11:47:19 PM

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

22
2/7/2012 10:42:42 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon