Python Forum

Full Version: Current time on x axis in matplolib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm monitoring and plotting temperature and pressure and would like to do so over the last 24 hours. I am getting the data from an Arduino sending the temp and pressure via the serial port in the following format "17.4 , 30.2". I can read and plot the data no problem but I can't figure out how to have the x axis show the current time on the right and the last 24 hours to the left. I have looked in the forums and some help files and have figured out how to get the hour and minutes but when I try to use either of those as my Xlim max it doesn't show the data. I think the problem is my data is not time stamped but goes by a sample count. I've tried a few things but am getting no where. I assume I'm not the first to do this, no sense reinventing the wheel.

What I would like is to have the current time on the far right of the graph, divided in 4 hour major ticks labeled with that time, minor ticks just showing no time attached. If the elapsed time goes past 12:00 midnight then the hours will show 8:00 of the previous day.

here's my code
# Graphing Temperature and Pressure

import serial # Import Serial Library
import numpy # Import numpy
import matplotlib.pyplot as plt #import matplot lib
from drawnow import *
import time
from datetime import datetime
from matplotlib.dates import DateFormatter, AutoDateFormatter, AutoDateLocator

tempArray = []
PressureArray = []

ArduinoData = serial.Serial('com8', 115200) # Read from Com8, that Mega is connect to at 115200 baud
plt.ion() # tell matplotlib you want iteractive mode to plot live data
count =0 # counts number of times loop runs

# Read Current Time
Current_time = time.localtime() #Gets current date and time
hour = Current_time[3] #Current hour
minute =Current_time[4] #Current minute
    
def makePlot(): #create a function that makes our desired plot    
    plt.ylim(0, 50) # sets min/max on y axis on graph
    plt.title('Live Temp and Pressure') # add title to graph
    plt.grid(True) # turn grid on
    plt.ylabel('Temp F') # Y axis label
    plt.plot(tempArray, 'ro-', label='Degrees F') # plots temp array as red with dots and connecting line
    plt.legend(loc='upper left') # puts Y axis line legend at top left of graph
    plt2=plt.twinx() # creates a second plot from first for pressure
    plt.ylim(27, 32) # set limit of 2nd Y axis
    plt2.plot(PressureArray, 'b^-', label = 'Pressure in "Hg') # plot Pressure with blue triangles and connecting line
    plt2.set_ylabel('Pressure "Hg') # adds label to second Y axis, have to use the set_label because of seconf plot
    plt2.ticklabel_format(useOffset=False) # use for when pressure is in Pascals keeps large number on 2nd Y axis scale
    plt2.legend(loc='upper right') # puts 2nd Y axis line legend at top right of graph

                            
while True: # forever while loop
    while (ArduinoData.inWaiting() == 0): # Wait for data in serial port
           pass # do nothing
    ArduinoString = ArduinoData.readline() # read data as a full string
    dataArray = ArduinoString.split(' , ') # split full string into parts separated by a ,
    temp = float (dataArray[0]) # convert first element in array to a float
    Pressure = float (dataArray[1]) # convert second element in array to a float
    
    tempArray.append(temp) # load temperature Array
    PressureArray.append(Pressure) # load Pressure Array
    drawnow (makePlot) # Call drawnow to update the live graph
    plt.pause(0.00001) # short pause needed for drawnow
    count = count + 1 # increments loop counter
    if(count > 50): # test if loop counts are above 50, defines length of X axis
        tempArray.pop(0) # removes first element of temp array
        PressureArray.pop(0) # removes first element of Pressure array
Thanks
John
Since you haven't saved time, you'll have to use the sample number as a linear tick on the x axis.
Or, if you know what time the measurements started and the interval between samples, then you can calculate the time.
for setting up and labeling x, y see: https://plot.ly/matplotlib/figure-labels/
I'm sorry but I can't follow what you've asked me to do. This is all new to me. I went on the link you provided and tried adding some of the custom legend lines but I can't figure it out.

Could you give me an example to work with?

Could I save the time as I'm reading the data in, (make a double array or a separate array?) and then use that as my x axis? The Arduino doesn't know what time it is so I would have to grab the time from my PC as I read the data.

Thanks for your help
John