Skip to content

Need to create and update figures in seperate cells? #425

@henrypinkard

Description

@henrypinkard

Something I've seen in previous versions as well as the current version: In order to have a figure that dynamically updates while some code is executing (e.g. tracking the loss function of an optimization), I have to create the figure in a separate jupyter notebook cell and run it before a I run the code that updates. If I don't do this, then a blank figure shows and never updates. I'm using Jupyter lab. Wondering if I'm doing something wrong here? It would make the code cleaner and easier to run if it could all be put in a single cell.

Example:

Using the following function:

def update_function(fig, ax):
   while True:
       # Do some computation
       ax.clear() #clear previously drawn
       ax.plot(some_data)
       fig.canvas.draw() #force it to update

This works:

# cell 1
fig, ax = plt.subplots()
# cell 2
update_function(fig, ax)

But this does not:

# one combined cell
fig, ax = plt.subplots()
update_function(fig, ax)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions