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

[Bug]: Removing the colorbar causes the size of axes to decrease · Issue #31084 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: Removing the colorbar causes the size of axes to decrease #31084

Closed as duplicate

Description

Bug summary

When creating and removing a colorbar with a ScalarMappable, the axes size unexpectedly changes, whereas removing a colorbar linked to a plotted pcolormesh does not affect the axes size.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm, colors

X, Y = np.meshgrid(np.linspace(-3, 3, 128), np.linspace(-3, 3, 128))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
fig, ax = plt.subplots()
pc = ax.pcolormesh(X, Y, Z, vmin=-1, vmax=1, cmap='RdBu_r')
width, height = ax.get_window_extent().width, ax.get_window_extent().height

cbar = fig.colorbar(pc, ax=ax); cbar.remove() # this does not change the axes size
assert ax.get_window_extent().width == width and ax.get_window_extent().height == height # pass

cbar = fig.colorbar(cm.ScalarMappable(norm=colors.NoNorm(), cmap=plt.get_cmap("viridis")), ax=ax); cbar.remove() # this unexpectedly changes the axes size
assert ax.get_window_extent().width == width and ax.get_window_extent().height == height # fail

Actual outcome

A plot with a smaller width:

Expected outcome

A plot with the original width:

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.8

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. anntzer commented on Feb 5, 2026

    Contributor

    I think this is a duplicate of #22257 and being worked on at #31040?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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