Matplotlib Multi-colored Title (Text) - in practice
<p>There is an example <a href="http://matplotlib.org/1.5.0/examples/text_labels_and_annotations/rainbow_text.html" rel="nofollow">here</a> for how to create a multi-colored text title.</p>

<p>However, I want to apply this to a plot that already has a figure in it. </p>

<p>For example, if I apply it to this (same code as with the example minus a few extras and with another figure)...:</p>

<pre><code>plt.rcdefaults()
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import transforms

fig = plt.figure(figsize=(4,3), dpi=300)

def rainbow_text(x,y,ls,lc,**kw):

    t = plt.gca().transData
    fig = plt.gcf()
    plt.show()

    #horizontal version
    for s,c in zip(ls,lc):
        text = plt.text(x,y," "+s+" ",color=c, transform=t, **kw)
        text.draw(fig.canvas.get_renderer())
        ex = text.get_window_extent()
        t = transforms.offset_copy(text._transform, x=ex.width, units='dots')

plt.figure()
rainbow_text(0.5,0.5,"all unicorns poop rainbows ! ! !".split(), 
        ['red', 'orange', 'brown', 'green', 'blue', 'purple', 'black'],
        size=40)
</code></pre>

<p>...the result is 2 plots with the title enlarged.
This sort of makes sense to me because I'm using plt. two times. 
But how do I integrate it so that it only refers to the first instance of plt. in creating the title?</p>

<p>Also, about this line:        </p>

<pre><code>t = transforms.offset_copy(text._transform, x=ex.width, units='dots')
</code></pre>

<p>I notice it can alter the spacing between words, but when I play with the values of x, results are not predictable (spacing is inconsistent between words). 
How can I meaningfully adjust that value?</p>

<p>And finally, where it says "units='dots'", what are the other options? Are 'dots' 1/72nd of an inch (and is that the default for Matplotlib?)?</p>

<p>How can I convert units from dots to inches? </p>

<p>Thanks in advance!</p>