Skip to content

[Bug]: figure.canvas.manager is set to None in between sequences of matplotlib calls #31809

@1234451515

Description

@1234451515

Bug summary

Very specific sequence of calls in ipython notebook results in backend being set to None, preventing figure from being displayed.

MatlotlibReproduceBug.ipynb

Code for reproduction

# Code is in attached notebook, copying cells here for awareness only, but NO guarantee that it will run or produce the error outside a notebook
%matplotlib ipympl
from matplotlib import pyplot as plt
import matplotlib as mpl
import numpy as np

print(mpl.get_backend())

# NEW CELL
fig, ax = plt.subplots(1, 1)
ax.plot(range(3))
fig.show()

# NEW CELL
np.random.randn(10000000)

# NEW CELL
fig, ax = plt.subplots(1, 1)
print(fig.canvas.manager)
print(mpl.get_backend())
ax.hist(range(3000))
ax.hist(range(300000))
print(fig.canvas.manager)
print(mpl.get_backend())
fig.show()

Actual outcome

Backend is correctly set for first figure. Second figure it is correctly set after creating figure, but after multiple calls to ax.hist it becomes None.

Image

Expected outcome

I would expect a figure backend to remain unchanged unless explicitly set to something different by the user

Additional information

Tested this on a slightly older version of ipython/matplotlib. Upgraded everything and the bug still exists.

This is a bit messy to reproduce, and seems to be very dependent on the amount of data passed to ax.hist as well as calls made previously.

Specific pattern of using fig, ax = plt.subplot(...) and fig.show() is one I settled on previously due to other issues with the ipympl backend failing to display figures in some cases.

Operating system

NAME="Amazon Linux" VERSION="2023" ID="amzn" ID_LIKE="fedora" VERSION_ID="2023" PLATFORM_ID="platform:al2023" PRETTY_NAME="Amazon Linux 2023.6.20250317" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023" HOME_URL="https://aws.amazon.com/linux/amazon-linux-2023/" DOCUMENTATION_URL="https://docs.aws.amazon.com/linux/" SUPPORT_URL="https://aws.amazon.com/premiumsupport/" BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023" VENDOR_NAME="AWS" VENDOR_URL="https://aws.amazon.com/" SUPPORT_END="2029-06-30"

Matplotlib Version

3.10.9

Matplotlib Backend

ipympl

Python version

Python 3.12.6

Jupyter version

7.5.3

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions