I recently used jupyter lab instead of jupyter notebook. But the following code can't work expectations.
import matplotlib.pyplot as plt
import numpy as np
from tqdm.notebook import tqdm, trange
#%matplotlib widget # For jupyter lab
%matplotlib notebook # For jupyter notebook
plt.ion()
fig, ax = plt.subplots()
xs = []
for i in trange(100):
x = i / 10
xs.append(x)
ax.clear()
ax.plot(xs, np.sin(xs))
fig.canvas.draw()
It works on the jupyter notebook, the plot will update dynamically. But It doesn't work on the jupyter lab. Of cause, the magic code of %matplotlib is changed on the individual environment.
By the way, I know another method to plot dynamically. This method also work jupyter lab. But this method can't work tqdm because clear_output will clear progress bar too. How to avoid this problem instead of the above question? I seem the below question is more simple than the above question.
import matplotlib.pyplot as plt
import numpy as np
from tqdm.notebook import tqdm, trange
from io import BytesIO
import imageio
from IPython.display import Image, display_png, clear_output
#%matplotlib widget
%matplotlib notebook
plt.ion()
fig, ax = plt.subplots()
xs = []
for i in trange(100):
x = i / 10
xs.append(x)
ax.clear()
ax.plot(xs, np.sin(xs))
io = BytesIO()
fig.savefig(io, format='png')
clear_output(wait=True)
display_png(Image(io.getvalue()))
Updated: The result of the above script is the following gif. This plot is dynamically rendering while running the script. (This script is not complitely because the tqdm progress bar is cleared. the problem is side effect of IPython.display.clear_output.)