matplotlib autoscale axes to include annotations
===

Does anyone know of an easy way to expand the plot area to include annotations? I have a figure where some labels are long and/or multiline strings, and rather than clipping these to the axes, I want to expand the axes to include the annotations.

Autoscale_view doesn't do it, and ax.relim doesn't pick up the position of the annotations, so that doesn't seem to be an option.

I've tried to do something like the code below, which loops over all the annotations (assuming they are in data coordinates) to get their extents and then updates the axes accordingly, but ideally I don't want my annotations in data coordinates (they are offset from the actual data points).

    xmin, xmax = plt.xlim()
    ymin, ymax = plt.ylim()
    # expand figure to include labels
    for l in my_labels:
        # get box surrounding text, in data coordinates
        bbox = l.get_window_extent(renderer=plt.gcf().canvas.get_renderer())
        l_xmin, l_ymin, l_xmax, l_ymax = bbox.extents
        xmin = min(xmin, l_xmin); xmax = max(xmax, l_xmax); ymin = min(ymin, l_ymin); ymax = max(ymax, l_ymax)
    plt.xlim(xmin, xmax)
    plt.ylim(ymin, ymax)

