在绘图上不显示时间,但显示正确的d

2024-05-16 22:54:33 发布

您现在位置:Python中文网/ 问答频道 /正文

没有在绘图上获得时间,但日期绘图正确,请检查列表dateIndex是否始终显示。
输出图如下所示:

xaxis

def graphData(dateIndex,irValue,mar):

    dateIndex.pop() #take out the empty string
    irValue.pop()  #equalising the size

    avgData = movingaverage(irValue,mar)
    spLen = len(dateIndex[mar-1:])

    fig = plt.figure()
    ax1 = plt.subplot(1,1,1)
    date = mdates.strpdate2num('%Y-%m-%d %H:%M:%S')
    datenum = [date(i) for i in dateIndex] # date function here.

    ax1.plot(datenum[-spLen:], avgData[-spLen:])
    ax1.grid(True)
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(20))
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d \n %H:%M:%S'))

    for label in ax1.xaxis.get_ticklabels():
            label.set_rotation(45)

    plt.show()

Tags: the绘图datepltpopmarsetax1