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

subfigures missing bbox_inches attribute in inline backend #19958

Closed
story645 opened this issue Apr 14, 2021 · 2 comments · Fixed by #19961
Closed

subfigures missing bbox_inches attribute in inline backend #19958

story645 opened this issue Apr 14, 2021 · 2 comments · Fixed by #19961
Labels
topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Milestone

Comments

@story645
Copy link
Member

story645 commented Apr 14, 2021

Bug report

Trying to add subplots to subfigures when using the jupyter inline backend triggers a missing bbox error. Specifically this code,

%matplotlib inline
import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(2, wspace=0.07)
axsLeft = subfigs[0].subplots()

where the backend complains AttributeError: 'SubFigure' object has no attribute 'bbox_inches'

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\Anaconda3\envs\mplrelease\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

~\Anaconda3\envs\mplrelease\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    246 
    247     if 'png' in formats:
--> 248         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    249     if 'retina' in formats or 'png2x' in formats:
    250         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~\Anaconda3\envs\mplrelease\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    130         FigureCanvasBase(fig)
    131 
--> 132     fig.canvas.print_figure(bytes_io, **kw)
    133     data = bytes_io.getvalue()
    134     if fmt == 'svg':

~\Anaconda3\envs\mplrelease\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2232             if bbox_inches:
   2233                 if bbox_inches == "tight":
-> 2234                     bbox_inches = self.figure.get_tightbbox(
   2235                         renderer, bbox_extra_artists=bbox_extra_artists)
   2236                     if pad_inches is None:

~\Anaconda3\envs\mplrelease\lib\site-packages\matplotlib\figure.py in get_tightbbox(self, renderer, bbox_extra_artists)
   1634 
   1635         for a in artists:
-> 1636             bbox = a.get_tightbbox(renderer)
   1637             if bbox is not None and (bbox.width != 0 or bbox.height != 0):
   1638                 bb.append(bbox)

~\Anaconda3\envs\mplrelease\lib\site-packages\matplotlib\figure.py in get_tightbbox(self, renderer, bbox_extra_artists)
   1653 
   1654         if len(bb) == 0:
-> 1655             return self.bbox_inches
   1656 
   1657         _bbox = Bbox.union(bb)

AttributeError: 'SubFigure' object has no attribute 'bbox_inches'

<Figure size 720x288 with 1 Axes>
@jklymak jklymak added the topic: geometry manager LayoutEngine, Constrained layout, Tight layout label Apr 14, 2021
@jklymak jklymak added this to the v3.4.2 milestone Apr 14, 2021
@jklymak
Copy link
Member

jklymak commented Apr 14, 2021

Just to keep ipython out of it:

import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(2, 1)
axsLeft = subfigs[0].subplots()
fig.savefig('Boo.png', bbox_inches='tight')

also triggers this error.

@jklymak
Copy link
Member

jklymak commented Apr 14, 2021

Oh, humm, this is an interesting problem. bbox_inches='tight' is supposed to expand the figure to encompass all the artists. However, what does that mean for a subfigure? If I have a label or legend that overspills the subfigure, is the parent supposed to get larger to make room?

That seems too complicated, and I suspect for now the bbox should just be the hard bbox that is a fixed size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants