Skip to content

[Bug]: ValueError: Unable to determine Axes to steal space for Colorbar. #23973

Closed

Description

@janosh

Bug summary

matplotlib==3.6.0 started raising an error when trying to add a colorbar to plt.hist():

ValueError: Unable to determine Axes to steal space for Colorbar. Either provide the cax argument to use as the Axes for the Colorbar, provide the ax argument to steal space from it, or add mappable to an Axes.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

xs = np.random.rand(100)

_, bin_edges, bars = plt.hist(xs)
color_map = getattr(plt.cm, "hot")
for x_val, rect in zip(bin_edges, bars.patches):
    rect.set_color(color_map(x_val))

cbar = plt.colorbar(
    plt.cm.ScalarMappable(cmap=color_map),
    # cax=ax.inset_axes([0.95, 0.1, 0.05, 0.8]),
)

Actual outcome

In matplotlib==3.6.0:

mpl==3 6 0

Expected outcome

In matplotlib==3.5.1:

mpl==3 5 1

Operating system

macOS 12.6

Matplotlib Version

3.6.0

Python version

3.10

Installation

pip

Activity

  1. janosh commented on Sep 21, 2022

    @janosh
    Author

    The error disappears in 3.6.0 by following the error message and passing cax=ax.inset_axes([0.95, 0.1, 0.05, 0.8]).

  2. jklymak commented on Sep 21, 2022

    @jklymak
    Member

    If it is ambiguous what axes to use, pass in the axes directly:

    cbar = plt.colorbar(
        plt.cm.ScalarMappable(cmap=color_map),
        ax=plt.gca()
    )
    

    You could make an axes, and use that, but you will lose some layout convenience.

  3. janosh commented on Sep 21, 2022

    @janosh
    Author

    Good to know I can keep auto-layout for the color bar.

    If it is ambiguous what axes to use, pass in the axes directly:

    What changed between 3.5.1 and 3.6.0? Why wasn't it ambiguous before?

  4. jklymak commented on Sep 21, 2022

    @jklymak
    Member

    This happened in #23740. However, I think we were under the impression this was deprecated, but it sounds like perhaps that was not the case?

  5. reopened this on Sep 23, 2022
  6. jvmunhoz commented on Sep 30, 2022

    @jvmunhoz

    I had the problem when trying to use create a simple SHAP plot using the shap package.

    import shap
    
    # use SHAP (SHapley Additive exPlanations) to explain the output of the generated model
    explainer = shap.TreeExplainer(model)
    shap_values = explainer.shap_values(X)
    shap.summary_plot(shap_values[1], X, feature_names=X.columns, show=False, max_display=20, plot_size=(15, 10))

    ValueError: Unable to determine Axes to steal space for Colorbar. Either provide the cax argument to use as the Axes for the Colorbar, provide the ax argument to steal space from it, or add mappable to an Axes.

    I had to downgrade to matplotlib 3.5.1 to fix the issue.

  7. jklymak commented on Sep 30, 2022

    @jklymak
    Member

    Please report to shap

    I will put on the agenda for next weeks call what we should do about this. It seems we screwed up the deprecation warning somehow, or a lot of downstream packages didn't see it.

  8. rcomer commented on Sep 30, 2022

    @rcomer
    Member

    There was a deprecation warning here, but it was only triggered if the mappable had an axes that was different from the current axes:

    if ax is None:
    ax = self.gca()
    if (hasattr(mappable, "axes") and ax is not mappable.axes
    and cax is None):
    _api.warn_deprecated(
    "3.4", message="Starting from Matplotlib 3.6, colorbar() "
    "will steal space from the mappable's axes, rather than "
    "from the current axes, to place the colorbar. To "
    "silence this warning, explicitly pass the 'ax' argument "
    "to colorbar().")

    In the OP's case, I think the mappable has no axes.

  9. added
    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
    on Sep 30, 2022
  10. added this to the v3.6.1 milestone on Sep 30, 2022
  11. jklymak commented on Sep 30, 2022

    @jklymak
    Member

    I guess this should get fixed for the bug release, though maybe not the most critical thing in the world. But we have probably broken a few libraries by doing this with no notice.

  12. TheGreatDeadOne commented on Sep 6, 2024

    @TheGreatDeadOne

    Is it possible to reopen this issue? I'm having the same problem with version 3.9.2. I can only plot my graphs (pyprocar and vaspkit) when I downgrade to version 3.4.2(3)

  13. jklymak commented on Sep 6, 2024

    @jklymak
    Member

    You can open a new issue, but the behaviour was deprecated, so unless there is a strong reason, you will have to either use old Matplotlib, or take the problem up with pyprocar and vaspkit.

  14. added a commit that references this issue on Mar 28, 2025
  15. added a commit that references this issue on Jul 31, 2026
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

    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: color/colorbar

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions