1

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.)

My desired result

elda
  • 21
  • 5
  • Does this answer your question? [jupyterlab interactive plot](https://stackoverflow.com/questions/50149562/jupyterlab-interactive-plot) – jayveesea Jun 08 '20 at 16:36
  • 1
    I tried it but my problem is not solved. I understood your suggested answer is interactive activities such as move displaying region or zoom plots after running the script. It is different from my problem. My problem is how to dynamically display plot while running the script. I seem dynamic is very complicated so I updated my question to display my desired result (This solution is not complete.). Thank you. – elda Jun 09 '20 at 12:49

0 Answers0