# Plot Classic List Threaded 7 messages Open this post in threaded view
|

## Plot

 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?RegardsParth _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Plot

 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)     y.append(data)     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, yvals 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] y = [yvals] # create a figure and plot the first x, y point at time 0 fig, ax = plt.subplots() pts = ax.plot(x, y, 'o') # 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 <[hidden email]> 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 > [hidden email] > https://mail.python.org/mailman/listinfo/matplotlib-users_______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Plot

 Awesome, thanks! parthaOn Sat, 3 Aug 2019 at 13:17, Scott Lasley <[hidden email]> wrote: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)     y.append(data)     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, yvals 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] y = [yvals] # create a figure and plot the first x, y point at time 0 fig, ax = plt.subplots() pts = ax.plot(x, y, 'o') # 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 <[hidden email]> 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 > [hidden email] > https://mail.python.org/mailman/listinfo/matplotlib-users _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Plot

 In reply to this post by matplotlib - users mailing list I got the solution as suggested by scott. My new problem is:Lets say I will generate lets  two pairs of 10 random numbers, say x and y.When first pair of x and y is generated, it will be plotted. Again when second set of number is generated, it will appear on the same graph and not on 2nd graph.This will continue till 10th pair.How to do this?RegardsParthOn 3 Aug 2019 1:17 pm, "Scott Lasley" <[hidden email]> wrote: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)     y.append(data)     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, yvals 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] y = [yvals] # create a figure and plot the first x, y point at time 0 fig, ax = plt.subplots() pts = ax.plot(x, y, 'o') # 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 <[hidden email]> 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 > [hidden email] > https://mail.python.org/mailman/listinfo/matplotlib-users _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Plot

 I guess adding a call to plt.figure() before each new figure should do it. Eventually plt.close() just before that if you want to close figures instead of keeping them open (but then you might also just be able to clear the current one instead of closing/opening). Le 14/08/2019 à 06:47, Partha Sinha a écrit : I got the solution as suggested by scott.  My new problem is: Lets say I will generate lets  two pairs of 10 random numbers, say x and y. When first pair of x and y is generated, it will be plotted. Again when second set of number is generated, it will appear on the same graph and not on 2nd graph. This will continue till 10th pair. How to do this? Regards Parth On 3 Aug 2019 1:17 pm, "Scott Lasley" <[hidden email]> wrote: 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)     y.append(data)     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, yvals 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] y = [yvals] # create a figure and plot the first x, y point at time 0 fig, ax = plt.subplots() pts = ax.plot(x, y, 'o') # 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 <[hidden email]> 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 [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users