Advertisement
regan254

Untitled

Jul 24th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import csv
  2. import random
  3. import time
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6. from itertools import count
  7. from matplotlib.animation import FuncAnimation
  8.  
  9.  
  10. x_value =0
  11. total_1 =1000
  12. total_2 = 1000
  13. field_names = ['x_value', 'total_1', 'total_2']
  14. with open('realtime.csv', 'w') as csv_file:
  15.     csv_writer = csv.DictWriter(csv_file, fieldnames = field_names)
  16.     csv_writer.writeheader()
  17. while True:
  18.     with open('realtime.csv', 'a')as csv_file:
  19.         csv_writer =csv.DictWriter(csv_file, fieldnames=field_names)
  20.         info = {
  21.             'x_value':x_value,
  22.             'total_1' : total_1,
  23.             'total_2': total_2
  24.         }
  25.         csv_writer.writerow(info)
  26.        
  27.         print(x_value, total_1, total_2)
  28.         x_value +=1
  29.         total_1 = total_1 + random.randint(-6,8)
  30.         total_2 = total_2 + random.randint(-5,6)
  31. time.sleep(1)
  32.  
  33. #Plotting the data
  34. index = count()
  35. def animate(i):
  36.     real = pd.read_csv('realtime.csv')
  37.     real['x_value']
  38.     x = real['x_value']
  39.     y1 = real['total_1']
  40.     y2 = real['total_2 ']
  41.    
  42.     plt.cla()
  43.     plt.plot(x, y1, label = 'totals 1')
  44.     plt.plot(x, y2, label = 'totals 2')
  45.  
  46.     plt.tight_layout()    
  47.     plt.legend(loc= ' upper left')
  48.  
  49. animated = FuncAnimation(plt.gcf(),animate, interval=1000)
  50. plt.tight_layout()
  51. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement