| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #!/usr/bin/env python3
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- from matplotlib import style
- import numpy as np
- import random
- import serial
- #initialize serial port
- ser = serial.Serial()
- ser.port = "COM3"
- # ser.port = '/dev/ttyS2' #Arduino serial port
- ser.baudrate = 115200
- ser.timeout = 10 #specify timeout when using readline()
- ser.open()
- if ser.is_open==True:
- print("\nAll right, serial port now open. Configuration:\n")
- print(ser, "\n") #print serial parameters
- # Create figure for plotting
- fig = plt.figure()
- ax = fig.add_subplot(1, 1, 1)
- xs = [] # time
- ys = [] # shoult temp
- rs = [] # is temp
- # This function is called periodically from FuncAnimation
- last_i = 1
- def animate(i, xs, ys):
- global last_i
- #Aquire and parse data from serial port
- line=ser.readline() #ascii
- try:
- line.decode()
- except:
- return
- line_as_list = line.split(b',')
- i = int(line_as_list[0].strip())
- if (i-last_i) < 1000:
- return
-
- print(line.decode().strip())
- last_i = i
- should_float = float(line_as_list[1].strip())
- is_float = float(line_as_list[2].strip())
-
- # Add x and y to lists
- xs.append(i)
- ys.append(should_float)
- rs.append(is_float)
- # Limit x and y lists to 20 items
- #xs = xs[-20:]
- #ys = ys[-20:]
- # Draw x and y lists
- ax.clear()
- ax.plot(xs, ys, label="Experimental Probability")
- ax.plot(xs, rs, label="Theoretical Probability")
- # Format plot
- plt.xticks(rotation=45, ha='right')
- plt.subplots_adjust(bottom=0.30)
- plt.title('This is how I roll...')
- plt.ylabel('Relative frequency')
- plt.legend()
- plt.axis([None, None, 0, 120]) #Use for arbitrary number of trials
- #plt.axis([1, 100, 0, 1.1]) #Use for 100 trial demo
- # Set up plot to call animate() function periodically
- ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys))
- plt.show()
|