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

AnchoredOffsetBox not taken into account by bbox_inches='tight' #2530

Closed
minrk opened this issue Oct 18, 2013 · 2 comments · Fixed by #2689
Closed

AnchoredOffsetBox not taken into account by bbox_inches='tight' #2530

minrk opened this issue Oct 18, 2013 · 2 comments · Fixed by #2689

Comments

@minrk
Copy link
Contributor

minrk commented Oct 18, 2013

I don't know if this is intended behavior, but an AnchoredOffsetBox outside the axes will be cut off if bbox_inches='tight'.

To reproduce:

from matplotlib.offsetbox import AnchoredOffsetbox, TextArea

ax = plt.gca()
ax.plot([0,1])
anchored_box = AnchoredOffsetbox(
    loc=6,
    child=TextArea("hello"),
    frameon=True,
    bbox_to_anchor=(1, 0.8),
    bbox_transform=ax.transAxes,
)
ax.add_artist(anchored_box)

plt.gcf().canvas.print_figure('test.png', format='png', bbox_inches='tight')

gives

test

This is reproduced with current master (8162371).

@minrk
Copy link
Contributor Author

minrk commented Oct 18, 2013

One symptom of this is yhat/ggpy#33, where ggplot figures have their legends cut off when using IPython's inline backend, which is implemented via print_figure(bbox_inches='tight').

@pelson
Copy link
Member

pelson commented Dec 19, 2013

Sorry for not looking at this sooner @minrk.
I extended the bbox_inches functionality a couple of months ago to handle every single artist that lives on the figure and the axes, so was really surprised to see this come up. Turns out that the problem is that the AnchoredOffsetbox artist claims to be clipping itself to a bbox which it isn't (i.e. the artist has been poorly implemented):

In [1]: import matplotlib.pyplot as plt

In [2]: from matplotlib.offsetbox import AnchoredOffsetbox, TextArea

In [3]: 

In [3]: ax = plt.gca()

In [4]: ax.plot([0,1])
Out[4]: [<matplotlib.lines.Line2D at 0x32f12d0>]

In [5]: anchored_box = AnchoredOffsetbox(
   ...:     loc=6,
   ...:     child=TextArea("hello"),
   ...:     frameon=True,
   ...:     bbox_to_anchor=(1, 0.8),
   ...:     bbox_transform=ax.transAxes)#

In [6]: anchored_box.set_clip_on(False)

In [7]: ax.add_artist(anchored_box)
Out[7]: <matplotlib.offsetbox.AnchoredOffsetbox at 0x32f5110>

In [8]: 

In [8]: plt.draw()

In [9]: print anchored_box.get_window_extent(plt.gcf().canvas.renderer).get_points()
[[ 584.        341.8     ]
 [ 627.190625  368.6     ]]

In [10]: print anchored_box.get_clip_on()
False

In [11]: print anchored_box.get_clip_box().get_points()
[[  80.   48.]
 [ 576.  432.]]

In [12]: anchored_box.set_clip_on(True)

In [13]: print anchored_box.get_window_extent(plt.gcf().canvas.renderer).get_points()
[[ 584.        341.8     ]
 [ 627.190625  368.6     ]]

In this case In [13] should be restricted to the clipping bounding box, but isn't.

As a workaround, simply turning clipping off on this artist (i.e. anchored_box.set_clip_on(False)) fixes the issue. I've also submitted a PR which fixes this by default #2689.

jankatins added a commit to jankatins/ggplot that referenced this issue Jan 19, 2014
This adds a workaround for a bug in the tight layout code in
matplotlib. The next version of matplotlib has the same fix
internaly, so this workaround is not needed for  matplotlib >1.3.1
matplotlib/matplotlib#2530

Closes: yhat#33
has2k1 pushed a commit to has2k1/plotnine that referenced this issue Apr 25, 2017
This adds a workaround for a bug in the tight layout code in
matplotlib. The next version of matplotlib has the same fix
internaly, so this workaround is not needed for  matplotlib >1.3.1
matplotlib/matplotlib#2530

Closes: yhat/ggpy#33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants