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());
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
Replace Invalid Values with None in Pandas Dataframe
Browse Files and Subfolders in Python
Numpy to Tfrecords: Is There a More Simple Way to Handle Batch Inputs from Tfrecords
How to Fix Character Constantly Accelerating in Both Directions After Deceleration Pygame
Pip Install Gives Error: Unable to Find Vcvarsall.Bat
Populate a Pandas Sparsedataframe from a Scipy Sparse Matrix
How to Display a 3D Plot of a 3D Array Isosurface in Matplotlib Mplot3D or Similar
How to Improve My Paw Detection
Insert an Item into Sorted List in Python
Is There a Multi-Dimensional Version of Arange/Linspace in Numpy
Running Bash Script from Within Python
How to Assign a Variable in an If Condition, and Then Return It
How to Efficiently Process a Numpy Array in Blocks Similar to Matlab's Blkproc (Blockproc) Function