8000 tight_layout support for subfigure by leejjoon · Pull Request #26108 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

tight_layout support for subfigure #26108

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/matplotlib/_tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _auto_adjust_subplotpars(
bb += [martist._get_tightbbox_for_layout_only(ax, renderer)]

tight_bbox_raw = Bbox.union(bb)
tight_bbox = fig.transFigure.inverted().transform_bbox(tight_bbox_raw)
tight_bbox = fig.transSubfigure.inverted().transform_bbox(tight_bbox_raw)

hspaces[rowspan, colspan.start] += ax_bbox.xmin - tight_bbox.xmin # l
hspaces[rowspan, colspan.stop] += tight_bbox.xmax - ax_bbox.xmax # r
Expand All @@ -97,22 +97,22 @@ def _auto_adjust_subplotpars(
margin_left = max(hspaces[:, 0].max(), 0) + pad_inch/fig_width_inch
suplabel = fig._supylabel
if suplabel and suplabel.get_in_layout():
rel_width = fig.transFigure.inverted().transform_bbox(
rel_width = fig.transSubfigure.inverted().transform_bbox(
suplabel.get_window_extent(renderer)).width
margin_left += rel_width + pad_inch/fig_width_inch
if not margin_right:
margin_right = max(hspaces[:, -1].max(), 0) + pad_inch/fig_width_inch
if not margin_top:
margin_top = max(vspaces[0, :].max(), 0) + pad_inch/fig_height_inch
if fig._suptitle and fig._suptitle.get_in_layout():
rel_height = fig.transFigure.inverted().transform_bbox(
rel_height = fig.transSubfigure.inverted().transform_bbox(
fig._suptitle.get_window_extent(renderer)).height
margin_top += rel_height + pad_inch/fig_height_inch
if not margin_bottom:
margin_bottom = max(vspaces[-1, :].max(), 0) + pad_inch/fig_height_inch
suplabel = fig._supxlabel
if suplabel and suplabel.get_in_layout():
rel_height = fig.transFigure.inverted().transform_bbox(
rel_height = fig.transSubfigure.inverted().transform_bbox(
suplabel.get_window_extent(renderer)).height
margin_bottom += rel_height + pad_inch/fig_height_inch

Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,12 @@ def __init__(self, parent, subplotspec, *,
self._set_artist_props(self.patch)
s 8000 elf.patch.set_antialiased(False)

def get_size_inches(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be added to the figure.pyi file.

How odd do we think that it is for SubFigure to have get_size_inches but not set_size_inches? I'm leaning towards that it is OK, with a small thought of "we should implement set_size_inches that raises and says you have to go talk to the (grand-)parent figure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think that this should be private (and have a similar private method for the top level Figure that the public get_size_inches calls, and then uses where we need the subfigure size can call the private method instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will introduce an private method (so no SubFigure.get_size_inches). @tacaswell : Do you think we still need to consider some sort of set_size_inches method (likely private) that shows a reasonable error message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the method is private, I do not have concerns about the missing setter.

w, h = self.figure.get_size_inches()
figbbox = self.figure.bbox
return (w*self.bbox.width/figbbox.width,
h*self.bbox.height/figbbox.height)

@property
def dpi(self):
return self._parent.dpi
Expand Down
0