0

What I'm trying to do here is display data for the previous 10 seconds only. Can someone help me understand why this code doesn't update the graph even though I alter the lists xData and yData that it refers to? The lists are never longer than 10 items but the graph does not 'shuffle' the x-axis over... how do i show only the data in the lists?

import random

xData = [0]
yData = [0]


def graph():
    plt.show()
    while True:
        try:
            if len(xData) < 11:
                xData.append(xData[len(xData) - 1] + 1)
                yData.append(random.randint(0, 20))
            else:
                xData.pop(0)
                yData.pop(0)
                xData.append(xData[len(xData) - 1] + 1)
                yData.append(random.randint(0, 20))

            plt.plot(xData, yData)
            plt.title('hello =)')
            plt.xlabel('time')
            plt.ylabel('groove')
            plt.pause(0.25)

        except:
            plt.close()
            break


while True:
    try:
        selection = input(
            "[1] Temperature\n[2] Light Level\n[3] Volume\n[4] Random filler bs\nSelect graph to show by number:\n")
        if selection == "4":
            graph()

    except KeyboardInterrupt:
        break
  • I think your desired output is similar to this: https://stackoverflow.com/a/71128728/8881141 - mainly, don't plot from scratch but update plotted data and axis limits. – Mr. T Apr 19 '22 at 15:01

0 Answers0