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

[Bug]: Removing colorbar misaligns other subplots · Issue #23502 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: Removing colorbar misaligns other subplots #23502

Closed as duplicate
Closed as duplicate

Description

Bug summary

When adding multiple colorbars with plt.colorbar to different subplots and then removing them again, the removal affects the width of the other subplot.

Code for reproduction

fig, ax = plt.subplots(2)
s = ax[0].scatter([0, 1, 2], [0, 1, 2], c=[0, 1, 2])
plt.colorbar(s, ax=ax[0])
cbar = plt.colorbar(s, ax=ax[1])
cbar.remove()

Actual outcome

Expected outcome

Additional information

This bug occurs with matplotlib>=3.4.0, matplotlib 3.3.4 produces the picture shown under "expected outcome", which is what I would assume is correct.

Operating system

macOS 11.0.1 and Ubuntu 18.04

Matplotlib Version

matplotlib 3.5.2

Matplotlib Backend

module://ipykernel.pylab.backend_inline

Python version

3.8.5

Jupyter version

4.6.3

Installation

pip

Activity

  1. oscargus commented on Jul 29, 2022

    Member

    Indeed! If anything, the bottom subplot should expand. Definitely not the top one.

  2. oscargus commented on Jul 29, 2022

    Member

    The problem seems to be that

    ax = self.mappable.axes

    points at the wrong axis, i.e. the top one. Which leads to that the rest of the code is applied to the top rather than the bottom axis.
    try:
    gs = ax.get_subplotspec().get_gridspec()
    subplotspec = gs.get_topmost_subplotspec()
    except AttributeError:
    # use_gridspec was False
    pos = ax.get_position(original=True)
    ax._set_position(pos)
    else:
    # use_gridspec was True
    ax.set_subplotspec(subplotspec)

    So self.mappable.axes is not set correctly(?).

  3. oscargus commented on Jul 29, 2022

    Member

    It is set correctly, it is "just" that the logic doesn't consider this case.

    The code assumes that the mappable provided (the scatter plot) is also the object that got the colorbar attached to it and therefore the part that should be updated when the colorbar is removed. Probably one should use ax instead here. (Althiguh that may cause other issues...)

  4. jklymak commented on Jul 29, 2022

    Member

    Thanks @oscargus that does indeed seem to be the problem. I'm skeptical we should resize anything, but if that is the existing behaviour we should be storing the result of ax=ax and operating on that (if it is a single axes).

    The problem is that the colorbar axes is created outside of colorbar, so the colorbar method never knows if it stole from space anyone or not. I think its overreach for colorbar to then try to give space back...

  5. jklymak commented on Dec 11, 2022

    Member

    Just to clarify here:

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots(2, layout='constrained')
    s = ax[0].scatter([0, 1, 2], [0, 1, 2], c=[0, 1, 2])
    plt.colorbar(s, ax=ax[0])
    
    plt.show()

    Does what I think the OP wants.

    If they really don't want to use constrained layout, they can also do various other hacks (inset_axes is the best way), or make a mock scalarmappable

    fig, ax = plt.subplots(2)
    s = ax[0].scatter([0, 1, 2], [0, 1, 2], c=[0, 1, 2])
    plt.colorbar(s, ax=ax[0])
    cbar = plt.colorbar(mpl.cm.ScalarMappable(norm=plt.Normalize(), cmap=plt.get_cmap('RdBu_r')),
    ax=ax[1])
    cbar.remove()
  6. github-actions commented on Feb 2, 2026

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  7. timhoffm commented on Feb 2, 2026

    Member

    Duplicate #22257

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

Metadata

Metadata

Assignees

No one assigned

    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