import matplotlib.pyplot as plt
x = [1,2,3,4] # x-coordinates of the data points
y = [4,7,6,8] # y-coordinates of the data points
graph = plt.plot(x,y) # plotting the data and storing the graph in variable named graph
plt.show() # showing the resultant graph
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import random
# initial data
x = [1]
y = [random.randint(1,10)]
# creating the first plot and frame
fig, ax = plt.subplots()
graph = ax.plot(x,y,color = 'g')[0]
plt.ylim(0,10)
# updates the data and graph
def update(frame):
global graph
# updating the data
x.append(x[-1] + 1)
y.append(random.randint(1,10))
# creating a new graph or updating the graph
graph.set_xdata(x)
graph.set_ydata(y)
plt.xlim(x[0], x[-1])
anim = FuncAnimation(fig, update, frames = None)
plt.show()
import matplotlib.pyplot as plt
import random
plt.ion() # turning interactive mode on
# preparing the data
y = [random.randint(1,10) for i in range(20)]
x = [*range(1,21)]
# plotting the first frame
graph = plt.plot(x,y)[0]
plt.ylim(0,10)
plt.pause(1)
# the update loop
while(True):
# updating the data
y.append(random.randint(1,10))
x.append(x[-1]+1)
# removing the older graph
graph.remove()
# plotting newer graph
graph = plt.plot(x,y,color = 'g')[0]
plt.xlim(x[0], x[-1])
# calling pause function for 0.25 seconds
plt.pause(0.25)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
# initial data
x = [random.randint(1,100)]
y = [random.randint(1,100)]
# creating the figure and axes object
fig, ax = plt.subplots()
# update function to update data and plot
def update(frame):
# updating the data by adding one more point
x.append(random.randint(1,100))
y.append(random.randint(1,100))
ax.clear() # clearing the axes
ax.scatter(x,y, s = y, c = 'b', alpha = 0.5) # creating new scatter chart with updated data
fig.canvas.draw() # forcing the artist to redraw itself
anim = FuncAnimation(fig, update)
plt.show()