question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need to create and update figures in seperate cells?

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
ianhicommented, Feb 4, 2022

as an aside I would recommend instead of clearing and redrawing every time you should use the set_data methods. e.g

line = ax.plot(some_data)[0]

# for updating
line.set_data(new_data)
fig.canvas.draw()
1reaction
ianhicommented, Feb 3, 2022

ahh and it turns out that there is a pretty simple workaround for the meantime

%matplotlib widget
from matplotlib import pyplot as plt
from time import sleep
import numpy as np

def display_immediately(fig):
    canvas = fig.canvas
    display(canvas)
    canvas._handle_message(canvas, {'type': 'send_image_mode'}, [])
    canvas._handle_message(canvas, {'type':'refresh'}, [])
    canvas._handle_message(canvas,{'type': 'initialized'},[])
    canvas._handle_message(canvas,{'type': 'draw'},[])
    
    
with plt.ioff():
    fig = plt.figure()

display_immediately(fig)

for i in range(10):
    x, y = np.random.random(2)
    plt.scatter(x, y)
    fig.canvas.draw()
    sleep(0.1)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Automatically Update Data in Another Sheet in Excel
Go to the destination worksheet and click the cell where you want to link the cell from the source worksheet.
Read more >
Update fields - Microsoft Support
Update all fields in a document · Press Ctrl + A. · Press F9. · If your document has tables with fields or...
Read more >
How to Update Table and Figure Numbers in Microsoft Word
Select Update entire table to update the table or figure numbers in your list of tables or figures.
Read more >
How to Update Table and Figure Numbers in Microsoft Word
These steps will also update your list of tables or list of figures. ... How to Create and Update a List of Tables...
Read more >
Word 2010-2007 Auto Update formulas in a table - YouTube
You cannot auto update formulas in word, but here is one step to reduce the concern in sending out quotations or invoices.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found