Line Plot not Plotting

Viewed 44

I'm trying to plot some data, however it's not plotting. When I run my code, it just says "process finished", with no plot showing up. I'm really not sure what I'm doing wrong here. Any advice on how to fix this?

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from random import randint
import time
import datetime

Time = time.time()
fig = plt.figure()

x = []
y = []


def rand_num():
    while True:
        n = randint(1, 1000)
        n1 = randint(1, 1000)
        n2 = randint(1, 1000)

        def animate(i):

                x.append(time.time())
                # print(Time)
                y.append(n)
                # print(n)
                plt.plot(x, y, color='r')
                # plt.plot(x, n1, color='g')
                # plt.plot(x, n2, color='b')
                euler = []
                return


        anim = animation.FuncAnimation(fig, animate,  interval=1000)
        plt.show()

1 Answers

Try calling the function at the end of the code:

rand_num()

enter image description here

For changing values try:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from random import randint
import time
import datetime

Time = time.time()
fig = plt.figure()

x = []
y = []

def rand_num():

    def animate(i):

        n = randint(1, 1000)
        n1 = randint(1, 1000)
        n2 = randint(1, 1000)
        x.append(time.time())
        y.append(n)
        plt.plot(x, y, color='r')
        euler = []

    anim = animation.FuncAnimation(fig, animate,  interval=1000)
    plt.show()

rand_num()

enter image description here

Related