0

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()
Salad96
  • 21
  • 1
  • How are you "switching to scenario 2"? – AKX Aug 01 '23 at 11:56
  • Anyway, see https://stackoverflow.com/questions/28269157/plotting-in-a-non-blocking-way-with-matplotlib – AKX Aug 01 '23 at 11:58
  • Does this answer your question? [Plotting in a non-blocking way with Matplotlib](https://stackoverflow.com/questions/28269157/plotting-in-a-non-blocking-way-with-matplotlib) – 9769953 Aug 01 '23 at 12:07
  • @AKX The switching behavior is through the socket connection, it workout perfectly if im not using plt.show(). The solution doesn't really work out for me, as Im using FuncAnimation function to do the animation plotting – Salad96 Aug 01 '23 at 12:32

0 Answers0