Jupyter notebook wrongly opened matplotlib interactive mode

The first cell of the notebook is

import matplotlib.pyplot as plt
plt.ioff()
print(plt.isinteractive())

This outputs False, as expected.
Then, the second part,

print(plt.isinteractive())
fig = plt.figure(figsize=(6.4, 9.6))
print(plt.isinteractive())

This outputs

False
True

<Figure size 640x960 with 0 Axes>

You can do it altogether within a .py file

import matplotlib.pyplot as plt
plt.ioff()
print(plt.isinteractive())
print(plt.isinteractive())
fig = plt.figure(figsize=(6.4, 9.6))
print(plt.isinteractive())

whose output, is

False
False
False

I tested on my WSL2 (ubuntu 24.04, python 3.12.3, matplotlib3.9.1) and on a server (centos 7, python 3.11.5, matplotlib 3.9.0), both give the same result.

Some other trial results to add:

  1. Single block
import matplotlib.pyplot as plt
plt.ioff()
print(plt.isinteractive())
print(plt.isinteractive())
fig = plt.figure(figsize=(6.4, 9.6))
print(plt.isinteractive())

outputs

False
False
True

<Figure size 640x960 with 0 Axes>
  1. ioff and creation of fig in the same cell; that is to say, the first cell only contains
import matplotlib.pyplot as plt

and the next cell is

print(plt.isinteractive())
plt.ioff()
print(plt.isinteractive())
fig = plt.figure(figsize=(6.4, 9.6))
print(plt.isinteractive())

This time I changed the order of plt.ioff and first output, but got the same result

False
False
True

<Figure size 640x960 with 0 Axes>

which, seems that only when fig is created, the interactive mode of matplotlib is opened. So it is a feature, not a bug? :sweat_smile:

Even if the second block is written as

with plt.ioff():
	fig = plt.figure(figsize=(6.4, 9.6))

there would still has output of <Figure size 640x960 with 0 Axes>.

Notebook seems to force open interactive mode when the first figure is created, unless manually closed by plt.close(fig) so that there is no figure output.
When plt.ioff() is manually set after first figure, no such output arises after second or later figures are created.