Python Forum
HeatMap plot with arduino serial data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HeatMap plot with arduino serial data
#1
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
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  1D plot frome 3D data layla 7 1,099 Jul-16-2023, 10:51 PM
Last Post: Pedroski55
  extract and plot data from a txt file usercat123 2 1,208 Apr-20-2022, 06:50 PM
Last Post: usercat123
  Searching Module to plot large data G_rizzle 0 1,422 Dec-06-2021, 08:00 AM
Last Post: G_rizzle
  Not able to figure out how to create bar plot on aggregate data - Python darpInd 1 2,253 Mar-30-2020, 11:37 AM
Last Post: jefsummers
  Digitizing heatmap image deepa 2 2,690 Mar-20-2020, 06:37 AM
Last Post: deepa
  Real-time plot from serial port Nochill_Senpai 1 5,354 Feb-12-2020, 03:04 AM
Last Post: Larz60+
  How to plot data to the same figure for every run? chry5ler 3 4,678 Aug-03-2018, 04:21 PM
Last Post: heras
  Reading data from Serial elgabry 1 2,556 Jul-05-2018, 05:10 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020