Skip to content

[Bug]: Colorbar cannot be added to another figure #25871

Closed
@Wrzlprmft

Description

@Wrzlprmft

Bug summary

Calling the colorbar method of one Figure object does not attach the colour bar to that figure, but to the one where the object informing the colour scale resides.

Code for reproduction

from matplotlib.pyplot import subplots
import numpy as np

fig1, axes1 = subplots()
fig2, axes2 = subplots()
fig1.suptitle("Figure 1")
fig2.suptitle("Figure 2")
image1 = axes1.imshow(np.random.random((9,9)))
image2 = axes2.imshow(np.random.random((9,9)))
fig2.colorbar(image1)

Actual outcome

The colour bar gets attached to Image 1:

colorbar_bug_1 colorbar_bug_2

Expected outcome

The colour bar should get attached to Image 2. At the very least there should be an error.

Matplotlib Version

3.7.1

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions