Getting a plot to update within a loop without using FuncAnimation

Hey all

I am trying to plot a graph which updates within a loop. Below is a simple example

%matplotlib ipympl

import matplotlib.pyplot as plt
import numpy as np
import time

x = []
y = []

fig, ax = plt.subplots()   

for i in np.arange(0, 10, 0.1):
    x.append(i)
    y.append(np.sin(i))
    ax.plot(x, y)
    time.sleep(.1)  # Add a delay to simulate real-time plotting

Now I know that this question has been asked a lot and while there are some good answers, many don’t work with JupyterLab, which is what I am using. The above code only displays the plot after the loop is over.

I can use the FuncAnimation function

%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

fig, ax = plt.subplots()

def animate(frame):
    x.append(frame)
    y.append(np.sin(frame))
    ax.plot(x, y)    
    return ax

ani = FuncAnimation(fig, animate, frames=np.arange(0, 10, 0.1), interval = 100, repeat = False)
ani;

and it does work like I expect it to but I was wondering if there was another way to update graphs. Since my main logic is inside the function called by FuncAnimation, I feel like it might be an anti-pattern.

I have seen how imshow() can be used to set_data and update it but all my attempts to set_data on the line returned by ax.plot has failed.

Is there a way to do it the way I am looking for? Or is FuncAnimation the only way.

P.S. I have mentioned it before but I am using the latest JupyterLab