Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import random
- import time
- import pandas as pd
- import matplotlib.pyplot as plt
- from itertools import count
- from matplotlib.animation import FuncAnimation
- x_value =0
- total_1 =1000
- total_2 = 1000
- field_names = ['x_value', 'total_1', 'total_2']
- with open('realtime.csv', 'w') as csv_file:
- csv_writer = csv.DictWriter(csv_file, fieldnames = field_names)
- csv_writer.writeheader()
- while True:
- with open('realtime.csv', 'a')as csv_file:
- csv_writer =csv.DictWriter(csv_file, fieldnames=field_names)
- info = {
- 'x_value':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)
- #Plotting the data
- index = count()
- def animate(i):
- real = pd.read_csv('realtime.csv')
- real['x_value']
- x = real['x_value']
- y1 = real['total_1']
- y2 = real['total_2 ']
- plt.cla()
- plt.plot(x, y1, label = 'totals 1')
- plt.plot(x, y2, label = 'totals 2')
- plt.tight_layout()
- plt.legend(loc= ' upper left')
- animated = FuncAnimation(plt.gcf(),animate, interval=1000)
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement