Skip to content

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

Closed

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

Activity

  1. added this to the v1.4.3 milestone on Nov 19, 2014
  2. itziakos commented on Dec 2, 2014

    @itziakos
    Contributor

    I am halfway through a solution for this issue:

    In short, the Annotation class, which sub-classes Text, does not implement the get_window_extent method so when the figure is trying to get the bounds of the contained artists will call the Text.get_window_extent method which has no clue about the arrow or patch_arrow attributes in Annotation.

  3. tacaswell commented on Dec 30, 2014

    @tacaswell
    Member

    @itziakos Any progress on this?

  4. itziakos commented on Jan 9, 2015

    @itziakos
    Contributor

    I was hopping that I could spent some time on it over the next two weeks. The main delay is related to making sure that there are tests checking the basic configurations for the annotation arrow. I can make a in-progress PR if it is desirable.

  5. tacaswell commented on Jan 9, 2015

    @tacaswell
    Member

    Just put 'WIP' in the title so we know it is a 'work in progress'.

    It is probably better to get feed back sooner rather than later.

    On Thu Jan 08 2015 at 7:29:09 PM Ioannis Tziakos notifications@github.com
    wrote:

    I was hopping that I could spent some time on it over the next two weeks.
    The main delay is related to making sure that there are tests checking the
    different configurations for the annotation arrow. I can make a in-progress
    PR if it is desirable.

    —
    Reply to this email directly or view it on GitHub
    #3816 (comment)
    .

  6. tacaswell commented on Jan 19, 2015

    @tacaswell
    Member

    This will be tied to #4012 as changing the get_window_extent of the annotation will require that in the setup of the arrow it call up to the Text version of get_window_extent.

  7. itziakos commented on Jan 21, 2015

    @itziakos
    Contributor

    Work in progress PR #4023

  8. tacaswell commented on Jan 31, 2015

    @tacaswell
    Member

    Should be fixed by 26c7d65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions