从对象“dateIndex”打印XLabel日期格式

2024-05-16 14:24:40 发布

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

我的对象列表“dateIndex”如下所示。。。你知道吗

2013-10-16 12:42:49
2013-10-16 12:42:49
2013-10-16 12:42:49
2013-10-16 12:42:49
2013-10-17 09:09:53
2013-10-17 09:10:40
2013-10-17 09:10:42
2013-10-17 09:20:02
2013-10-17 09:30:02
2013-10-17 09:40:02
2013-10-17 09:48:52
2013-10-17 09:59:01

我需要把这些日期和时间写在x标签上,但我不知道怎么写。你知道吗

我的绘图功能:

def graphData(dateIndex,irValue):

    fig = plt.figure()
    ax1 = plt.subplot2grid((5,4), (0,0), rowspan=4, colspan=4)#, axisbg='#07000d')
    ax1 = plt.subplot(1,1,1)

    date = mdates.strpdate2num('%d-%m-%Y %H:%M:%S')
    converters = { dateIndex:mdates.strpdate2num('%d-%m-%Y %H:%M:%S') }
    #this is where i get an error -> unhashable type: 'list'
    ax1.plot(date,irValue)
    ax1.grid(True)
    ax1.spines['bottom'].set_color("#5998ff")
    ax1.tick_params(axis='y')
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
    ax1.axis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y %H:%M:%S'))

Tags: 对象绘图列表date时间plt标签set
1条回答
网友
1楼 · 发布于 2024-05-16 14:24:40

您正在创建mdates.strpdate2num函数,但从未将其应用于实际的日期字符串。你知道吗

下面给出了一个固定的graphData函数,其中使用了列表理解来应用该函数。我还修复了函数中的另外两个问题,即:应该使用plot_date而不是plot,并且在设置格式化程序时错过了xaxis中的x。你知道吗

def graphData(dateIndex,irValue):

    fig = plt.figure()
    ax1 = plt.subplot2grid((5,4), (0,0), rowspan=4, colspan=4)#, axisbg='#07000d')
    ax1 = plt.subplot(1,1,1)

    date = mdates.strpdate2num('%Y-%m-%d %H:%M:%S')

    datenum = [date(i) for i in dateIndex] # Apply your date function here.

    ax1.plot_date(datenum, irValue)
    ax1.grid(True)
    ax1.spines['bottom'].set_color("#5998ff")
    ax1.tick_params(axis='y')
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y %H:%M:%S'))

    fig.autofmt_xdate()

    plt.show()

相关问题 更多 >