| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 | #!/usr/bin/env python3import matplotlib.pyplot as pltimport matplotlib.animation as animationfrom matplotlib import styleimport numpy as npimport randomimport serial#initialize serial portser = serial.Serial()ser.port = "COM3"# ser.port = '/dev/ttyS2' #Arduino serial portser.baudrate = 115200ser.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 plottingfig = plt.figure()ax = fig.add_subplot(1, 1, 1)xs = [] # time ys = [] # shoult temprs = [] # is temp# This function is called periodically from FuncAnimationlast_i = 1def 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 periodicallyani = animation.FuncAnimation(fig, animate, fargs=(xs, ys))plt.show()
 |