FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

AnchoredOffsetBox not taken into account by bbox_inches='tight' · Issue #2530 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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

This is reproduced with current master (8162371).

Activity

  1. minrk commented on Oct 18, 2013

    ContributorAuthor

    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').

  2. pelson commented on Dec 19, 2013

    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL