Code Not Printing



The animate function doesn’t create a graph,which is the problem, the data is being generated in the first part of the code

Thanks

Uffdah, that’s hard to read. Please post your actual code and some description of what’s going on. It helps us help you. Thanks

import csv
import random
import time

x_value = 0
total_1 = 1000
total_2 = 1000

fieldnames=["x_values","total_1","total_2"]

with open('data.csv','w') as csv_file:
    csv_writer=csv.DictWriter(csv_file,fieldnames=fieldnames)
    csv_writer.writeheader()
    
while True:
    with open('data.csv','a') as csv_file:
        csv_writer=csv.DictWriter(csv_file,fieldnames=fieldnames)
    
        info = {
    
            "x_values":x_value,
            "total_1":total_1,
            "total_2":total_2
        }
    
        csv_writer.writerow(info)
        print(x_value,total_1,total_2)
    
        x_value += 1
        total_1 = total_1 + random.randint(-6,8)
        total_2 = total_2 + random.randint(-5,6)
    
    time.sleep(1)

    ```

This creates the data

import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
%matplotlib notebook
from IPython import display

plt.style.use('fivethirtyeight')

index=count()

def animate(i):
    data=pd.read_csv('data.csv')
    x=data['x_value']
    y1=data['total_1']
    y2=data['total_2']
    plt.cla()
    plt.plot(x,y1,label='Channel1')
    plt.plot(x,y2,label='Channel2')
    plt.legend()
    
    
anim=FuncAnimation(plt.gcf(),animate,interval=1000)


plt.tight_layout()
plt.show()

This is supposed to display the data, but Jupyter Notebook is not even executing this code

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.