How To Draw In The Same Figure Each Loop
How to update a plot on same figure during the loop?
Nosotros tin use matplotlib to update a plot on every iteration during the loop. With the help of matplotlib.pyplot.describe() office we tin can update the plot on the same effigy during the loop.
Using matplotlib.pyplot.draw(): It is used to update a figure that has been changed. It volition redraw the current figure.
Syntax: figure.canvas.draw()
Before this nosotros use figure.ion() function to run a GUI event loop. Without using figure.ion() we may not be able to come across the GUI plot.
Approach:
In the given instance firstly nosotros are importing all the necessary libraries that we are going to employ. And then creating X and Y. X holds the values from 0 to 10 which evenly spaced into 100 values. due east.g. we are creating values from 2 to 3 with evenly spaced 5 values (np.linspace(2, three, v)) It should output like these five values from 2 to 3 evenly spaced array([two , 2.25, two.five , 2.75, 3 ]). Afterward that we are initializing GUI using plt.ion() function, now we take to create a subplot, then we can plot Ten and Y values. After that we are running a for loop up to some iterations and creating a new_y values which concord our updating value then we are updating the values of X and Y using set_xdata() and set_ydata(). And canvass.draw() volition plot the updated values and canvas.flush_events() holds the GUI upshot till the UI events have been candy. This will run till the loop ends and values will be updated continuously.
Code:
Python3
import numpy as np
import time
import matplotlib.pyplot as plt
x = np.linspace( 0 , 10 , 100 )
y = np.sin(x)
plt.ion()
figure, ax = plt.subplots(figsize = ( 10 , viii ))
line1, = ax.plot(x, y)
plt.championship( "Geeks For Geeks" , fontsize = xx )
plt.xlabel( "X-centrality" )
plt.ylabel( "Y-centrality" )
for _ in range ( 50 ):
new_y = np.sin(x - 0.v * _)
line1.set_xdata(x)
line1.set_ydata(new_y)
figure.sail.draw()
figure.canvass.flush_events()
time.sleep( 0.1 )
Output:
Updating plot
Here, figure.sail.flush_events() is used to clear the former figure before plotting the updated figure.
Example 2: In this example lawmaking, nosotros are updating the value of y in a loop using set_xdata() and redrawing the figure every fourth dimension using canvas.draw().
Python3
from math import pi
import matplotlib.pyplot as plt
import numpy equally np
import time
x = np.linspace( 1 , thousand , 5000 )
y = np.random.randint( one , 1000 , 5000 )
plt.ion()
fig = plt.figure()
ax = fig.add_subplot( 111 )
line1, = ax.plot(x, y)
plt.xlabel( "X-axis" )
plt.ylabel( "Y-axis" )
plt.title( "Updating plot..." )
for _ in range ( fifty ):
line1.set_xdata(ten * _)
line1.set_ydata(y)
fig.canvass.draw()
fig.canvass.flush_events()
time.sleep( 0.1 )
Output:
Updating plot.
Source: https://www.geeksforgeeks.org/how-to-update-a-plot-on-same-figure-during-the-loop/
Posted by: delacruzlinto1997.blogspot.com

0 Response to "How To Draw In The Same Figure Each Loop"
Post a Comment