Oct-08-2018, 10:57 AM
Hi,
I am porting arduino serial data in form of comma separated values in python shell.
When i am receiving these values in the python shell. I am plotting a heatmap from it. The challenge i am facing in my code is of synchronization. How i should synchronize each cell with each data value coming from the serial monitor.
Shivam
I am porting arduino serial data in form of comma separated values in python shell.
When i am receiving these values in the python shell. I am plotting a heatmap from it. The challenge i am facing in my code is of synchronization. How i should synchronize each cell with each data value coming from the serial monitor.
Shivam
import numpy as np import matplotlib import matplotlib.pyplot as plt import serial import time tempdata = serial.Serial("COM8",38400,timeout=0.1) strn = [] rows = ["A","B","C","D"] columns = ["1", "2", "3", "4", "5", "6", "7","8"] tempsArray = np.empty((4,8)) plt.ion() fig, ax = plt.subplots() # The subplot colors do not change after the first time # if initialized with an empty matrix im = ax.imshow(np.random.rand(4,8),cmap='plasma') # Axes ticks ax.set_xticks(np.arange(len(columns))) ax.set_yticks(np.arange(len(rows))) # Axes labels ax.set_xticklabels(columns) ax.set_yticklabels(rows) # Rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") ax.set_title("") fig.tight_layout() text = [] while True: #Makes a continuous loop to read values from Arduino tempdata.flush() strn = tempdata.readline()[:] #reads the value from the serial port as a string tempsString = np.asarray(strn) tempsFloat = np.fromstring(tempsString, dtype=float, sep= ',') tempsArray.flat=tempsFloat print(tempsArray) im.set_array(tempsArray) #Delete previous annotations for ann in text: ann.remove() text = [] #Loop over data dimensions and create text annotations. for i in range(len(rows)): for j in range(len(columns)): text.append(ax.text(j, i, tempsArray[i, j], ha="center", va="center", color="b")) # allow some delay to render the image plt.pause(0.1) plt.ioff() [output][output][output][output][output][output][output][output][output][output][output][output][output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output][/output]