[Matplotlib-users] Plot
Scott Lasley
selasley at icloud.com
Sat Aug 3 03:47:45 EDT 2019
This should do what you want. It works in a jupyter notebook but not in jupyterlab. It also works as a standalone python script if you comment out the %matplotlib notebook line.
Best regards,
Scott
# based on https://matplotlib.org/gallery/animation/animate_decay.html#sphx-glr-gallery-animation-animate-decay-py
# but uses a zip iterator for the frames variable in FuncAnimation instead of the data_gen generator function
# used in the example
# comment out this line if you are not running this in a jupyter notebook
%matplotlib notebook
# %matplotlib nbagg
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
def init_plot():
# if you know the data ranges you can set fixed
# xlim and ylim limits here, otherwise uncomment
# the ax.relim and ax.autoscale_view calls in
# draw_pts to rescale the axes based on the
# last data point plotted
ax.set(xlim=[0, 11], ylim=[0, 120])
return []
def update_pts(data):
x.append(data[0])
y.append(data[1])
pts.set_data(x, y)
# ax.relim()
# ax.autoscale_view(scaley=True)
return [pts]
# data to plot. replace with your data values
xvals = np.arange(1, 11)
yvals = np.array([1, 4, 9, 16, 25, 36, 49, 64, 81, 100])
# zip xvals[1:] and yvals[1:] since we are plotting
# xvals[0], yvals[0] at time 0 after we create the figure
xy_points = zip(xvals[1:], yvals[1:])
# create x and y lists containing the first point to be plotted.
# update_pts will append the next x and y values from the iterator xy_points
# to x and y every 5000ms and update the pts being plotted
x = [xvals[0]]
y = [yvals[0]]
# create a figure and plot the first x, y point at time 0
fig, ax = plt.subplots()
pts = ax.plot(x, y, 'o')[0]
# loop through the remaining points and update the plot every
# 5000ms
anim = FuncAnimation(fig=fig,
func=update_pts,
frames=xy_points,
init_func=init_plot,
interval=5000,
blit=False,
repeat=False)
plt.show()
> On Aug 3, 2019, at 1:04 AM, Partha Sinha <pnsinha68 at gmail.com> wrote:
>
> I want to plot a list of 10 numbers. I want to plot each point after 5 secs (first point in o sec and 2nd point after 5 sec anf so on) and want to plot them in same graph.
> How to do?
> Regards
> Parth
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
More information about the Matplotlib-users
mailing list