Python matplotlib轴格式

Python matplotlib轴格式,python,matplotlib,pyqt,Python,Matplotlib,Pyqt,我正在matplotlib中创建一个实时绘图,hpwever我无法让x轴实时更新刻度,我想做的是获得每个刻度的发生时间,例如,如果将刻度设置为5分钟间隔,则它将是10:20、10:25、10:30等。我当前所做的不起作用,我将新时间附加到数组中,然后将数组调用到xtick。 数组: xticks: self.ax1.set_xticklabels(self.date) 如果这对你有意义,请告诉我。我把这个例子放在一起,它不是最漂亮的。我认为关键是使用绘图时间(ars…)告诉matplotlib

我正在matplotlib中创建一个实时绘图,hpwever我无法让x轴实时更新刻度,我想做的是获得每个刻度的发生时间,例如,如果将刻度设置为5分钟间隔,则它将是
10:20、10:25、10:30等
。我当前所做的不起作用,我将新时间附加到数组中,然后将数组调用到xtick。 数组:

xticks:

self.ax1.set_xticklabels(self.date)

如果这对你有意义,请告诉我。我把这个例子放在一起,它不是最漂亮的。我认为关键是使用绘图时间(ars…)告诉matplotlib正确查找数字和格式

使用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)
产生:

更新: 我刚刚找到了你的另一个密码,我猜你正在使用

试着替换

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

现在,您可以将时间戳传递给matplotlib,而不是

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
试着做一些类似的事情

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
通过适当的导入和变量初始化

from datetime import datetime, timedelta

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

你必须更改XTICK还是可以重新缩放轴?我必须更改XTICK,因为轴是实时的。也许我不理解这个问题。您试图绘制什么(即数据看起来像什么)?您是否试图创建一个仅显示5分钟窗口的“滚动”绘图?最后,据我所知,
setxticklabels
只会更改文本标签,而不会更改x轴比例。我拥有的是一个实时绘图,它会一直持续到用户退出程序,绘图是一个线条图,如果您是windows,请查看任务管理器/性能,我的绘图与他们的,除了我想在xticks上加上时间,所以基本上你想要一个在x轴上有一定时间范围的移动图?我将尝试组合一些内容。@user1582983,这样我上面发布的代码就可以在我的机器上运行(windows、python 2.7.2、matplotlib等)。那么,你有什么不同的做法?(顺便说一句,你提供的细节越多,你的回答就越好)它给了我一个错误,
dt=datetime.datetime.fromordinal(ix)ValueError:ordinal必须>=1
@user1582983,欢迎你,我很高兴它能工作!y轴是否随时间改变比例?我不确定self.fig.canvas.draw()是否也会更新它。
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
from datetime import datetime, timedelta

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