I'm trying to execute two scenario with different figure, When I'm running the first one, the animation go without a problem. But it doesn't allow me to have a switch between scenario. When I'm trying to switch to the scenario2, I have to close the figure manually for the second one to show up. The reason I'm using the plt.show() is that I want the animation plot to run indefinitely, until the next flag is present. Any suggestion on this?
 while True:
            if not flag_queue.empty():
                queue_item = flag_queue.get()
                if isinstance(queue_item, int): 
                    current_flag = queue_item      
            if current_flag == 1:
                if prev_flag != current_flag:
                    print("Sine Wave")
                    prev_flag = current_flag
                    fig, ax = plt.subplots()
                    x = np.linspace(0, 4, 1000)
                    y = np.sin(2 * np.pi * x)
                    line, = ax.plot(x, y)
                    ani = animation.FuncAnimation(fig, update_sin, fargs=(line,), interval=25, blit=True, cache_frame_data=False)
                    plt.show()
                    
            elif current_flag == 2:
                if prev_flag != current_flag:
                    plt.close()
                    print("Cosine Wave")
                    prev_flag = current_flag
                    fig, ax = plt.subplots()
                    x = np.linspace(0, 4, 1000)
                    y = np.cos(2 * np.pi * x)
                    line, = ax.plot(x, y)
                    ani = animation.FuncAnimation(fig, update_sin, fargs=(line,), interval=25, blit=True, cache_frame_data=False)
                    plt.show()
