title: Live plotting in Jupyter Lab 3 using Matplotlib
body: <p>I want to dynamically update the plot of a cell. I.e. the plot is initialized at the beginning of the cell, and updated in a (computationally heavy) for-loop, showing how the computation is progressing. In <code>jupyter notebook</code>, this can be done using <a href="https://stackoverflow.com/a/34486703/9318372">pneumatics solution</a> in <a href="https://stackoverflow.com/questions/34486642/what-is-the-currently-correct-way-to-dynamically-update-plots-in-jupyter-ipython">What is the currently correct way to dynamically update plots in Jupyter/iPython?</a></p>
  <pre class="lang-py prettyprint-override"><code>%matplotlib notebook
  
  import numpy as np
  import matplotlib.pyplot as plt
  import time
  
  def pltsin(ax, colors=['b']):
      x = np.linspace(0,1,100)
      if ax.lines:
          for line in ax.lines:
              line.set_xdata(x)
              y = np.random.random(size=(100,1))
              line.set_ydata(y)
      else:
          for color in colors:
              y = np.random.random(size=(100,1))
              ax.plot(x, y, color)
      fig.canvas.draw()
  
  fig,ax = plt.subplots(1,1)
  ax.set_xlabel('X')
  ax.set_ylabel('Y')
  ax.set_xlim(0,1)
  ax.set_ylim(0,1)
  for f in range(5):
      pltsin(ax, ['b', 'r'])
      time.sleep(1)
  </code></pre>
  <p>I am looking for an equivalent way of doing it in <code>jupyter lab</code>. I tried replacing <code>%matplotlib notebook</code> with <code>%matplotlib widget</code>, using the <a href="https://github.com/matplotlib/ipympl" rel="nofollow noreferrer"><code>ipympl</code></a> library, but that didn't work: The figure only shows once the loop is finished.</p>
  <p>What I do not want are solutions like the one proposed by <a href="https://stackoverflow.com/a/52672859/9318372">Ziofil</a> in or the one by <a href="https://stackoverflow.com/a/66246240/9318372">Paidoo</a> in <a href="https://stackoverflow.com/questions/50149562/jupyterlab-interactive-plot">jupyterlab interactive plot</a> which clear the whole output, as I might print additional things such as e.g. a tqdm progress bar</p>