Skip to content

[Bug]: Incorrect behavior in nbagg when calling plt.figure(num=1) #21957

Open
@ianhi

Description

@ianhi

Bug summary

From matplotlib/ipympl#405 it seems that if you repreatedly run a cell with plt.figure(num=1) and then plot a new line you will only ever have a plot with one line on it.

Code for reproduction

Cell 1

%matplotlib nbagg
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi/2, np.pi/2)
i=0

Cell 2 (run this one repeatedly)

plt.figure(num=1)
plt.plot(x, np.sin(x*i))
i += 1

Actual outcome

nbagg-bug

Expected outcome

Do the equivalent from ipython:

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
x = np.linspace(-np.pi/2, np.pi/2)

# each iteration of loop represents a cell re-execution
i = 0
def run_cell():
    global i
    plt.figure(num=1)
    plt.plot(x, np.sin(x*i))
    i += 1

correct-ipython

Additional information

No response

Operating system

No response

Matplotlib Version

3.5.1

Matplotlib Backend

nbagg

Python version

3.9.7

Jupyter version

6.4.4

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions