Align Text (Relative)

I am currently using the annotate() method for my data points and I was curious if there is a way to center a line of text relative to a line of text below it. I am currently using two annotate() function calls in a row (I need the text to be different colors) but I need the first one to act as a title for the second (i.e. so I want it centered relative to the one below.) I have tried to use the length of the second bit of text to center but I just cannot seem to do it. The code looks sort of like this:

import matplotlib.pyplot as plt

annotateTitle=‘Title’
annotateText=‘Blah, Blah, Blah’
plt.annotate(annotateTitle, xy=(1,1), xytext=(20,50), xycoords=‘data’, textcoords=‘offset points’)
plt.annotate(annotateText, xy=(1,1), xytext=(20,20), xycoords=‘data’, textcoords=‘offset points’, size=‘small’, color=‘black’)

Any ideas?

-Andy

Andrew Kelly wrote:

I am currently using the annotate() method for my data points and I was curious if there is a way to center a line of text relative to a line of text below it. I am currently using two annotate() function calls in a row (I need the text to be different colors) but I need the first one to act as a title for the second (i.e. so I want it centered relative to the one below.) I have tried to use the length of the second bit of text to center but I just cannot seem to do it. The code looks sort of like this:

import matplotlib.pyplot as plt
.......
annotateTitle='Title'
annotateText='Blah, Blah, Blah'
plt.annotate(annotateTitle, xy=(1,1), xytext=(20,50), xycoords='data', textcoords='offset points')
plt.annotate(annotateText, xy=(1,1), xytext=(20,20), xycoords='data', textcoords='offset points', size='small', color='black')

In the example at the bottom of http://matplotlib.sourceforge.net/users/annotations.html

you see an example where the horizontalalignment is specified. Can you just do something like the following?

plt.annotate(annotateTitle, xy=(1,1), xytext=(20,50), xycoords='data', textcoords='offset points', horizontalalignment='center')
plt.annotate(annotateText, xy=(1,1), xytext=(20,20), xycoords='data', textcoords='offset points', size='small', color='black', horizontalalignment='center')

You also see examples of the alignment parameters at http://matplotlib.sourceforge.net/examples/pylab_examples/annotation_demo.html#pylab-examples-annotation-demo

Thanks,

Jason