plot

I want help on graph
I need to plot numbers one by one (lets say after interval of 5 sec) but
all in the same graph. How to do ?
My code is below

import time
import matplotlib.pyplot as plt
import numpy as np
from random import randint
i = 1
numbers = [0]*5
while i < 5:
     numbers[i] = randint(0,100)
     plt.plot(np.arange(1,6),numbers, 'o')
     i += 1
     matplotlib.pyplot.draw()
     time.sleep(5)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190531/5c95c885/attachment.html>

Try using plt.pause instead of time.sleep. This may do what you want

import matplotlib.pyplot as plt
from random import randint

numbers = [0]*5
# draw the initial points
oldlines = plt.plot(range(1, 6), numbers, 'o', c='r')
plt.pause(5)
for i in range(5):
    numbers[i] = randint(0,100)
    # remove the previously plotted lines before drawing the new ones
    oldlines[0].remove()
    oldlines = plt.plot(range(1, 6), numbers, 'o', c='r')
    plt.pause(5)

···

On May 31, 2019, at 5:07 AM, Partha Sinha <pnsinha68 at gmail.com> wrote:

I want help on graph
I need to plot numbers one by one (lets say after interval of 5 sec) but all in the same graph. How to do ?
My code is below

import time
import matplotlib.pyplot as plt
import numpy as np
from random import randint
i = 1
numbers = [0]*5
while i < 5:
     numbers[i] = randint(0,100)
     plt.plot(np.arange(1,6),numbers, 'o')
     i += 1
     matplotlib.pyplot.draw()
     time.sleep(5)
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

It worked. However, I was looking for in one graph but here it's producing
n no of graphs. can we make it in the graph ?
partha

···

On Fri, 31 May 2019 at 21:10, Scott Lasley <selasley at icloud.com> wrote:

Try using plt.pause instead of time.sleep. This may do what you want

import matplotlib.pyplot as plt
from random import randint

numbers = [0]*5
# draw the initial points
oldlines = plt.plot(range(1, 6), numbers, 'o', c='r')
plt.pause(5)
for i in range(5):
    numbers[i] = randint(0,100)
    # remove the previously plotted lines before drawing the new ones
    oldlines[0].remove()
    oldlines = plt.plot(range(1, 6), numbers, 'o', c='r')
    plt.pause(5)

> On May 31, 2019, at 5:07 AM, Partha Sinha <pnsinha68 at gmail.com> wrote:
>
> I want help on graph
> I need to plot numbers one by one (lets say after interval of 5 sec) but
all in the same graph. How to do ?
> My code is below
>
>
> import time
> import matplotlib.pyplot as plt
> import numpy as np
> from random import randint
> i = 1
> numbers = [0]*5
> while i < 5:
> numbers[i] = randint(0,100)
> plt.plot(np.arange(1,6),numbers, 'o')
> i += 1
> matplotlib.pyplot.draw()
> time.sleep(5)
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190531/8b88ed53/attachment.html&gt;

Are you making the plots in a jupyter notebook? The code worked for me in ipython and when saved as a file and run from the command line, but it does produce multiple plots in a jupyter notebook.

plt.pause does not seem to work in a notebook. The documentation for plt.pause says "This can be used for crude animation. For more complex animation, see matplotlib.animation." The snippet below is based on the matplotlib simple animation example (https://matplotlib.org/examples/animation/basic_example.html) It works in a jupyter notebook if you use the magic
%matplotlib notebook
or
%matplotlib nbagg
but it does not work in jupyter lab since those magics are not supported.

%matplotlib nbagg

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

def draw_pts(i):
    y[i] = np.random.randint(10, 100)
    pts.set_data(x, y)
    ax.relim()
    ax.autoscale_view(scaley=True)
    return [pts]

def init_pts():
    return

x = np.arange(1, 6)
# set y to nan's so no points are drawn initially
y = np.full(5, np.nan)

fig, ax = plt.subplots()
ax.set(xlim=[0, 6])
pts = ax.plot(x, y, 'o')[0]

anim = FuncAnimation(fig,
                     draw_pts,
                     frames=5,
                     init_func=init_pts,
                     interval=2000,
                     blit=True,
                     repeat=False)

Notes: If init_func is not set, draw_pts is called twice with i = 0, so I used a dummy init_pts function. ax.autoscale_view works in the jyputer notebook but the y axis does not update properly if the code is run in ipython on my mac. If repeat=True, draw_pts will be called forever with i ranging from 0 to frames - 1 repeatedly. interval sets the delay between updates in milliseconds.

hth,
Scott

···

On May 31, 2019, at 12:11 PM, Partha Sinha <pnsinha68 at gmail.com> wrote:

It worked. However, I was looking for in one graph but here it's producing n no of graphs. can we make it in the graph ?
partha

On Fri, 31 May 2019 at 21:10, Scott Lasley <selasley at icloud.com> wrote:
Try using plt.pause instead of time.sleep. This may do what you want

import matplotlib.pyplot as plt
from random import randint

numbers = [0]*5
# draw the initial points
oldlines = plt.plot(range(1, 6), numbers, 'o', c='r')
plt.pause(5)
for i in range(5):
    numbers[i] = randint(0,100)
    # remove the previously plotted lines before drawing the new ones
    oldlines[0].remove()
    oldlines = plt.plot(range(1, 6), numbers, 'o', c='r')
    plt.pause(5)

> On May 31, 2019, at 5:07 AM, Partha Sinha <pnsinha68 at gmail.com> wrote:
>
> I want help on graph
> I need to plot numbers one by one (lets say after interval of 5 sec) but all in the same graph. How to do ?
> My code is below
>
>
> import time
> import matplotlib.pyplot as plt
> import numpy as np
> from random import randint
> i = 1
> numbers = [0]*5
> while i < 5:
> numbers[i] = randint(0,100)
> plt.plot(np.arange(1,6),numbers, 'o')
> i += 1
> matplotlib.pyplot.draw()
> time.sleep(5)
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> Matplotlib-users Info Page