0

I'm trying to plot live data on Python from an arduino through the serial port.

I found some difficulties with the annotate function: I don't know how to configure it or whether the place where I have put the annotate function is the right place or not.

import serial
import time
import numpy
import matplotlib.pyplot as plt
from drawnow import *

temperature= []
vitesse= []
charge= []
current= []
Time = []
cnt=0

# Create our serial object
arduinoData = serial.Serial('com5', 9600) 

# Turn on matplotlib interactive mode to plot live data
plt.ion() 

##fig1 = plt.figure()
STARTINGTIME = round(time.time(),2)

# A function that makes our desired plot
def makeFig(): 
    plt.subplot(2,2,1)
    plt.subplot(2,2,1).annotate(str(temperature)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming Temperature Sensor Data')
    plt.ylabel('Temperature C')
    plt.grid(True)
    plt.plot(temperature, 'ro-')

    plt.subplot(2,2,2)
    plt.subplot(2,2,2).annotate(str(vitesse)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming Speed Sensor Data')
    plt.ylabel('Speed KM/H')
    plt.grid(True)
    plt.plot(vitesse, 'bo-')

    plt.subplot(2,2,3)
    plt.subplot(2,2,3).annotate(str(charge)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming SOC Sensor Data')
    plt.ylabel('Battery Charge %')
    plt.grid(True)
    plt.plot(charge, 'go-')

    plt.subplot(2,2,4)
    plt.subplot(2,2,4).annotate(str(current)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming Current Sensor Data')
    plt.ylabel('Current A')
    plt.grid(True)
    plt.plot(current, 'yo-')

while True:

    while (arduinoData.inWaiting()==0): 
        # Wait here until there is data
        pass

    # Read the line of text from the serial port
    arduinoString = arduinoData.readline() 
    # Split it into an array
    dataArray = arduinoString.split(';')   
    temp = float(dataArray[0])
    vite = float(dataArray[1])
    char = float(dataArray[2])
    curr = float(dataArray[3])

    # Build our temperature array by appending temperature readings
    temperature.append(temp) 
    # Build our vitesse array by appending temp readings
    vitesse.append(vite)                    
    # Build our charge array by appending temp readings 
    charge.append(char)                     
    # Build our current array by appending temp readings
    current.append(curr)                     

    Time.append(round(time.time(),2) - STARTINGTIME)

    # Update our live graph
    drawnow(makeFig)                       

    plt.pause(0.00001)
    cnt += 1
    if(cnt > 50):
        temperature.pop(0)
        vitesse.pop(0)
        charge.pop(0)
        current.pop(0)
Michael Currie
  • 13,721
  • 9
  • 42
  • 58

1 Answers1

0

annotate takes at least two arguments. The first is the text and the second is a tuple containing the coordinates where you want the text to go. Additionally, since you want to annotate every point, you will need to call annotate inside a loop.

Here's an example:

import matplotlib.pyplot as plt
import numpy as np
Y = np.sin(np.linspace(0, 3, 10))
plt.plot(Y)
for i, y in enumerate(Y):
    plt.annotate(str(y), (i, y))
plt.show()

Simple plot with annotated points

Here's where the loop should go in your code:

def makeFig(): #Create a function that makes our desired plot

    plt.subplot(2,2,1)
    plt.title('Live Streaming Temperature Sensor Data')
    plt.ylabel('Temperature C')
    plt.grid(True)
    plt.plot(temperature, 'ro-')
    for i, t in enumerate(temperature):
        plt.annotate(str(t), (i, t), textcoords='offset points')
Amy Teegarden
  • 3,842
  • 20
  • 23