Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ianhi opened this issue Dec 15, 2021 · 3 comments
Open

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

ianhi opened this issue Dec 15, 2021 · 3 comments

Comments

@ianhi
Copy link
Contributor

ianhi commented Dec 15, 2021

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

@jklymak
Copy link
Member

jklymak commented Dec 15, 2021

That seems like the expected and desired behaviour to me. For a notebook, you definitely don't want the result to depend on how many times you ran a cell.

@QuLogic
Copy link
Member

QuLogic commented Dec 15, 2021

plt.figure(num=1) makes the figure with num=1 active. Re-running the cell first clears its outputs, which closes the figure. Thus, there is no figure with num=1 and a new one is created.

So I think I agree with @jklymak that this is expected and intended.

@jklymak
Copy link
Member

jklymak commented Dec 15, 2021

Just to be clear, if you want to access an already-open figure, you can stash it as an object and continue to operate on it from other cells.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants