Jupyterlab Interactive Plot

jupyterlab interactive plot

As per Georgy's suggestion, this was caused by Node.js not being installed.

matplotlib widget disappears after first use

It works when activate the matplotlib interactive support every time by moving the magic command into the second cell:

%matplotlib widget
plt.figure(1)
x = np.arange(100)
y = x*x
plt.plot(x,y)
plt.show()

How to update interactive figure in loop with JupyterLab

You can use asyncio, taking advantage of the IPython event loop:

import matplotlib.pyplot as plt
import asyncio
%matplotlib widget
fig = plt.figure()

async def update():
for i in range(5):
print(i)
x = list(range(i + 2))
xx = [x**2 for x in x]
plt.clf()
plt.plot(x, xx)
fig.canvas.draw()
await asyncio.sleep(1)

loop = asyncio.get_event_loop()
loop.create_task(update());

gif with a notebook with single cell being run and the animation progressing as expected

Can I show a plot created in another file on JupyterLab?

Yes, it is possible after all!

%matplotlib widget needs to be used at the start of the notebook and since the class method will be called from another function (on a button.on_click event), it is possible to use the @out.capture() decorator above it so that the plt.show() gets displayed. It's also possible to make the figure a class attribute to be able to have more control.

So here's a bit of working code if someone would like to replicate:

Notebook

%matplotlib widget
from ipywidgets import Button, Output
from myfile import MyClass

out = Output()

example_button = Button(
description='Example',
disabled=False,
button_style='',
tooltip='Click me'
)

@out.capture()
def on_example_button_clicked(b):
example_button.disabled = True
myclass = MyClass()
myclass.create_plot()
out.clear_output(wait=True)
display(myclass.fig.canvas)
example_button.disabled = False

example_button.on_click(on_example_button_clicked)

display(example_button)
display(out)

myfile.py

import matplotlib.pyplot as plt

class MyClass():
def __init__(self):
plt.ioff() # otherwise it'll also show inside logs
plt.clf()
self.fig = plt.figure()

def create_plot(self):
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')


Related Topics



Leave a reply



Submit