Skip to content

[Bug]: Error when removing colorbar in constrained layout #20978

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

Closed
mfrodl opened this issue Sep 2, 2021 · 3 comments · Fixed by #20980
Closed

[Bug]: Error when removing colorbar in constrained layout #20978

mfrodl opened this issue Sep 2, 2021 · 3 comments · Fixed by #20980
Labels
Milestone

Comments

@mfrodl
Copy link

mfrodl commented Sep 2, 2021

Bug summary

When calling remove() on a colorbar that belongs to a constrained layout, subsequent plt.show() call produces a traceback, without showing anything in the plot.

The scenario worked in version 3.3.4, the error appeared first in 3.4.0.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

figure, axes = plt.subplots(1, 1, constrained_layout=True)

data = np.random.normal(size=(10,10))
im = axes.imshow(data)
colorbar = figure.colorbar(im, ax=axes)
colorbar.remove()

plt.show()

Actual outcome

Traceback (most recent call last):
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\backends\backend_qt5.py", line 475, in _draw_idle
    self.draw()
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\backends\backend_agg.py", line 406, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\figure.py", line 2781, in draw
    self.execute_constrained_layout(renderer)
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\figure.py", line 3122, in execute_constrained_layout
    do_constrained_layout(fig, renderer, h_pad, w_pad, hspace, wspace)
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\_constrained_layout.py", line 109, in do_constrained_layout
    _make_layout_margins(fig, renderer, h_pad=h_pad, w_pad=w_pad,
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\_constrained_layout.py", line 241, in _make_layout_margins
    cbpos, cbbbox = _get_pos_and_bbox(cbax, renderer)
  File "C:\Users\mfrodl\Envs\myenv\lib\site-packages\matplotlib\_constrained_layout.py", line 451, in _get_pos_and_bbox
    pos = pos.transformed(fig.transSubfigure - fig.transFigure)
AttributeError: 'NoneType' object has no attribute 'transSubfigure'

Expected outcome

Plot window with the image, without colorbar.

Operating system

Windows 10

Matplotlib Version

3.4.3

Matplotlib Backend

Qt5Agg

Python version

3.8.3

Jupyter version

No response

Other libraries

No response

Installation

pip

Conda channel

No response

@jklymak
Copy link
Member

jklymak commented Sep 2, 2021

Thanks for the clear report. Sorry this broke - its funny that it worked before, but adding the extra layer of indirection from the new subfigure functionality made this no longer work. #20980 should fix it - if you have the ability to pull from GitHub let us know if it works for you.

@QuLogic QuLogic added this to the v3.4.4 milestone Sep 2, 2021
@mfrodl
Copy link
Author

mfrodl commented Sep 3, 2021

The proposed fix works fine for me. Thanks a lot!

@jklymak
Copy link
Member

jklymak commented Sep 3, 2021

Thanks a lot for checking!

@QuLogic QuLogic modified the milestones: v3.4.4, v3.5.0 Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants