draw bbox around axes title


I'm trying to draw a box around an axes title, and I'm having trouble
controlling it. I want the box to span the width of the x axis. I
tried some iterations on the below since xy and height/width are
hard-coded in set_title, but when I call show, it seems the width of
the box is reset. Any ideas? Thanks,


import matplotlib as mpl
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, axisbg='lightgrey')


rd = np.random.dirichlet(np.ones(10))
ax.plot(np.arange(10), rd, 'bo', markersize=5)
ax.vlines(np.arange(10), 0 ,rd, color='b')

title_box = dict(boxstyle = "Round", fc='gray', ec='black')
ax.set_title("1", bbox = title_box, x=0, y=1)
bbox = ax.title._bbox_patch

fig.suptitle("Dirichlet(1) Draw", fontsize=36)

Yes. Because the location of the texts are backend-dependent, the
location of the xbox surrounding the text are recalculated during the
drawing time. I don't think there is no easy workaround unfortunately.

If you are experienced in matplotlib, you may try is to rewrite the
get_path method of the bbox. The example code below, while a bit
complicated, tries to make the the bbox align with the axes.

bbox_get_path = bbox.get_path
def my_get_path():
    pad = bbox.get_boxstyle().pad*bbox.get_mutation_scale()
    x0, y0 = bbox.get_transform().transform_point((0,0))
    xx = ax.bbox.x0 - x0
    return bbox_get_path()
bbox.get_path = my_get_path




On Fri, Jan 6, 2012 at 4:56 AM, Skipper Seabold <jsseabold@...287...> wrote:

but when I call show, it seems the width of
the box is reset.