user1582983
user1582983

Reputation:

matplotlib axes formatting

I am creating a realtime plot in matplotlib, hpwever I cannot get the x axis to update the ticks in realtime, what I want to do is have the time of occurence on each tick, for example if the ticks were set up to 5 minute intervals it would be 10:20,10:25,10:30,etc. What Im currently doing doesn't work, i append new times to the array and then call the array to the xtick. array:

self.datetime1 = time.localtime()
self.datetime1 = timeString  = time.strftime("%m-%d %H:%M:%S", self.datetime1)
self.date.append(self.datetime1)

xticks:

self.ax1.set_xticklabels(self.date)

Upvotes: 0

Views: 1096

Answers (1)

Onlyjus
Onlyjus

Reputation: 6149

Let me know if this makes sense to you. I put this example together, it is not the prettiest. I think the key is to use the plot_time(ars...) to tell matplotlib to look for numbers and format correctly.

Using python[2.7.2], matplotlib, numpy:

import numpy as np
from matplotlib import pyplot as plt
import random, sys
from datetime import datetime, timedelta
import time

tWindow=1 #moving window in minutes

timeList=[datetime.now()]
valList=[random.randint(1, 20)]

fig = plt.figure() #Make a figure
ax = fig.add_subplot(111) #Add a subplot

#Create the line with initial data using plot_date to add time to the x axis
line,=plt.plot_date(timeList, valList, linestyle='-') 

#Set the x limits to the time window
ax.set_xlim([datetime.now()-timedelta(seconds=tWindow*60),datetime.now()])

#set the y limits
ax.set_ylim(0,20)

#grab the blank background to clear the plot later
background = fig.canvas.copy_from_bbox(ax.bbox)

#show the figure
fig.show()

#loop
for i in range(100):
    #restore the background
    fig.canvas.restore_region(background)

    #add time to time list
    timeList.append(datetime.now())

    #add random value to values
    valList.append(random.randint(1, 20))

    #update the line data
    line.set_data(timeList,valList)

    #update x limits
    ax.set_xlim([datetime.now()-timedelta(seconds=tWindow*60),datetime.now()])

    #redraw widnow
    fig.canvas.draw()

    #pause the loop for .5 seconds
    time.sleep(0.5)

Produces: code output

Update:

I just found your other post with the code I guess you are working on.

Try replacing

self.l_user, = self.ax.plot([],self.user, label='Total %')

With

self.l_user, = self.ax.plot_date([],self.user, label='Total %')

Now you can pass timestamps to matplotlib so instead of

def timerEvent(self, evt):
        # get the cpu percentage usage
        result = self.get_cpu_usage()
        # append new data to the datasets
        self.user.append(result[0])
        # update lines data using the lists with new data
        self.l_user.set_data(range(len(self.user)), self.user)
        # force a redraw of the Figure
        self.fig.canvas.draw()
           #else, we increment the counter
        self.cnt += 1

Try doing something along the lines of

def timerEvent(self, evt):
        # get the cpu percentage usage
        result = self.get_cpu_usage()
        # append new data to the datasets
        self.user.append(result[0])
        #save the current time
        self.timeStamp.append(datetime.now())
        # update lines data using the lists with new data
        self.l_user.set_data(self.timeStamp, self.user)
        #rescale the x axis maintaining a 5 minutes window
        self.ax.set_xlim([datetime.now()-timedelta(seconds=5*60),datetime.now()])
        # force a redraw of the Figure, this might not update the x axis limits??
        self.fig.canvas.draw()
           #else, we increment the counter
        self.cnt += 1

with the appropriate imports and variable initialization

from datetime import datetime, timedelta

class CPUMonitor(FigureCanvas):
    """Matplotlib Figure widget to display CPU utilization"""
    def __init__(self):
        ...
        self.timeStamp=[]
        ...

Upvotes: 1

Related Questions