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

axes.get_tightbbox doesn't include legends... #9130

Closed
jklymak opened this issue Aug 30, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@jklymak
Copy link
Contributor

commented Aug 30, 2017

Bug report

The bbox returned by axes.get_tightbbox doesn't include legends that overspill the axis.

Bug summary

This is an easy fix, but wasn't sure if the behavior was desired:

Code for reproduction

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
h = ax.plot(range(10), label='boo')
print(ax.get_tightbbox(fig.canvas.get_renderer()))
hh = ax.legend(h, 'Boooo', loc='center left', bbox_to_anchor=(0.9, 0.5))
print(ax.get_tightbbox(fig.canvas.get_renderer()))
print(hh._legend_box.get_window_extent(renderer))

Actual outcome

Bbox(x0=61.40277777777777, y0=29.077777777777776, x1=579.5, y1=425.9)
Bbox(x0=61.40277777777777, y0=29.077777777777776, x1=579.5, y1=425.9)
Bbox(x0=540.288888888889, y0=212.48888888888885, x1=659.413888888889, y1=262.71111111111105)

Expected outcome

The second bbox could have x1=659.41 to include the legend.

solution

in axes/_bases.py we could change:

     for child in self.get_children():
            if isinstance(child, OffsetBox) and child.get_visible():
                bb.append(child.get_window_extent(renderer))
            elif isinstance(child, Legend) and child.get_visible():
                 bb.append(child._legend_box.get_window_extent(renderer))

@jklymak jklymak referenced this issue Aug 30, 2017

Merged

[MRG] Constrained_layout (geometry manager) #9082

18 of 18 tasks complete
@WeatherGod

This comment has been minimized.

Copy link
Member

commented Aug 30, 2017

@jklymak

This comment has been minimized.

Copy link
Contributor Author

commented Aug 30, 2017

@WeatherGod This is just designed to catch legends that are children of the axes object. This won't do anything with legends that are associated w/ figures.

@jklymak

This comment has been minimized.

Copy link
Contributor Author

commented Nov 26, 2017

Closed by #9164

@jklymak jklymak closed this Nov 26, 2017

@QuLogic QuLogic added this to the v2.2 milestone Nov 26, 2017

@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.