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?
Regards
Parth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190803/8cb3824c/attachment.html>

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” selasley@icloud.com 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[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@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@python.org

https://mail.python.org/mailman/listinfo/matplotlib-users

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" <selasley@icloud.com > <mailto:selasley@icloud.com>> 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
    <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@gmail.com > <mailto:pnsinha68@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

Can the codes be shared?

···

On 3 Aug 2019 1:17 pm, “Scott Lasley”
selasley@icloud.com 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](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@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

Hi Partha,

This stackoverflow answer should help you do what you want

Here is some similar code. Run the code between the ...'s in a Jupiter notebook cell to create the first empty plot
...

# comment out this line if you are not running this in a jupyter notebook
%matplotlib notebook

import numpy as np
from matplotlib import pyplot as plt

def update_points(fig, pts, new_data):
    # get the data for the points in the current plot
    pts_data = pts.get_data()
    # update the points using set_data with the
    # new_data values appended to the current values
    pts.set_data(np.append(pts_data[0], [new_data[0]]),
                 np.append(pts_data[1], [new_data[1]]))
    # update the plot
    fig.canvas.draw_idle()
    # uncomment the next two lines if you want the plot to
    # automatically adjust its limits when you add data
# ax.relim()
# ax.autoscale_view(scaley=True)
    return

# create a figure
# if you are running the code in ipython you may need to
# call plt.ion() after creating the figure
fig, ax = plt.subplots()

# set the plot's x and y limits if you know the range
# of the data you will generate. otherwise let
# update_plot adjust the limits
ax.set(xlim=[0,100], ylim=[0,100])

# plot an empty list to create the pts Line2D array
# with no data
pts = ax.plot(, 'o')[0]

...

Then in the next cell(s) call update_points for each new data point to be plotted

...
new_data = [10, 10]
update_points(fig=fig, pts=pts, new_data=new_data)
...
new_data = [20, 40]
update_points(fig=fig, pts=pts, new_data=new_data)
...

Hope this helps,
Scott

···

On Aug 14, 2019, at 12:47 AM, Partha Sinha <pnsinha68@gmail.com> wrote:

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

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users