Skip to content

savefig with bbox_inches='tight' ignores annotation without text #3816

Closed
@breedlun

Description

@breedlun

It appears that the bbox_inches='tight' option in savefig ignores annotations without text. Here is my example code

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0.2, 0.2, 0.6, 0.6])
arrow1 = ax.annotate('text', xy = [0.5,-0.2], xycoords = 'axes fraction', \
    xytext = [-72,0], textcoords = 'offset points', \
    arrowprops = dict(arrowstyle = '-|>', mutation_scale = 10.0, \
    shrinkA = 0, shrinkB = 0, linewidth = 1))
arrow2 = ax.annotate('', xy = [0.5,1.2], xycoords = 'axes fraction', \
    xytext = [-72,0], textcoords = 'offset points', \
    arrowprops = dict(arrowstyle = '-|>', mutation_scale = 10.0, \
    shrinkA = 0, shrinkB = 0, linewidth = 1))
fig.savefig('test.png')
fig.savefig('test-tight.png', bbox_inches = 'tight')

Here is test.png, showing that I have two annotations. One annotation, with text, below the axes and one annotation, without text, above the axes.

Here is test-tight.png, showing only one annotation. The annotation above the axes, without the text, has been ignored.

Looking in the source code, bbox_inches='tight' attempts to find the size and location of artists by calling artist.get_window_extent(). When I try arrow1.get_window_extent(), I get a bounding box that appears to correspond to the text. When I try arrow2.get_window_extent(), I get a bounding box with zero height and zero width. Thus, the root of the problem is .get_window_extent() doesn't include the arrow.

Any ideas for how to get around this, in a reasonably robust manner? If I could somehow get the proper bounding box for the whole annotation, then I would be in business. However, I can't even seem to get the line or patch objects out of arrow2.

In case it matters, I am on matplotlib 1.4.0, Python 2.7.6, and Mac OS X 10.8.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions