Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
breedlun opened this issue Nov 19, 2014 · 7 comments
Closed

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

breedlun opened this issue Nov 19, 2014 · 7 comments

Comments

@breedlun
Copy link
Contributor

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

@itziakos
Copy link
Contributor

itziakos commented Dec 2, 2014

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.

@tacaswell
Copy link
Member

@itziakos Any progress on this?

@itziakos
Copy link
Contributor

itziakos commented Jan 9, 2015

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.

@tacaswell
Copy link
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)
.

@tacaswell
Copy link
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.

@itziakos
Copy link
Contributor

Work in progress PR #4023

@tacaswell
Copy link
Member

Should be fixed by 26c7d65

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

No branches or pull requests

4 participants