How to update a plot on same figure during the loop?
We can use matplotlib to Plot live data with Matplotlib. With the help of matplotlib.pyplot.draw() function we can update the plot on the same figure during the loop.
Plotting live data with Matplotlib
Using matplotlib.pyplot.draw(), It is used to update a figure that has been changed. It will redraw the current figure. Before this we use figure.ion() function to run a GUI event loop. Without using figure.ion() we may not be able to see the GUI plot.
In the given example firstly we are importing all the necessary libraries. And create X and Y. X holds the values from 0 to 10 which evenly spaced into 100 values. After that we are initializing GUI using plt.ion() function, now we have to create a subplot. After that, we are running a for loop and create new_y values which hold our updating value then we are updating the values of X and Y using set_xdata() and set_ydata(). The canvas.draw() will plot the updated values and canvas.flush_events() holds the GUI event till the UI events have been processed. This will run till the loop ends and values will be updated continuously.
Here, figure.canvas.flush_events() is used to clear the old figure before plotting the updated figure.
In this example, we are updating the value of y in a loop using set_xdata() and redrawing the figure every time using canvas.draw().
Please Login to comment...