Matplotlib: xticks every 15 minutes, starting on the hour

后端 未结 2 1284
孤街浪徒
孤街浪徒 2020-12-10 15:38

I am trying to plot values of temperature against time with the time formatted as HH:MM. I am able to set the xticks to recur every 15 minutes but the first tick is at the f

相关标签:
2条回答
  • 2020-12-10 16:21

    You could tell the MinuteLocator to only use the minutes 0,15,30,45 using the byminute argument.

    xlocator = md.MinuteLocator(byminute=[0,15,30,45], interval = 1)
    
    0 讨论(0)
  • 2020-12-10 16:24

    Use Axes.set_xticklabels,

    labels = ['04:45', '05:00', '05:15']
    plt.gca().set_xticklabels(labels)
    

    which produces,


    Shift time to the concurrent quarter-hours,

    import datetime
    
    list_dt = [ datetime.datetime(2017,2,15,4,40),
                datetime.datetime(2017,2,15,4,46),
                datetime.datetime(2017,2,15,4,52),
                datetime.datetime(2017,2,15,4,58),
                datetime.datetime(2017,2,15,5,4),
                datetime.datetime(2017,2,15,5,10)]
    
    adjust_list_dt = list()
    
    for dt in list_dt:
        print(dt.minute//15)
    
        if dt.minute % 15 == 0:
            adjust_minutes = dt.minute
        else:
            adjust_minutes = (dt.minute//15 + 1)*15
    
        if adjust_minutes == 60:
            adjust_list_dt.append(dt.replace(hour=dt.hour+1, minute=0))
        else:
            adjust_list_dt.append(dt.replace(minute=adjust_minutes))
    
    print(adjust_list_dt)
    # Output
    '''
    [datetime.datetime(2017, 2, 15, 4, 45), 
     datetime.datetime(2017, 2, 15, 5, 0),
     datetime.datetime(2017, 2, 15, 5, 0), 
     datetime.datetime(2017, 2, 15, 5, 0), 
     datetime.datetime(2017, 2, 15, 5, 15), 
     datetime.datetime(2017, 2, 15, 5, 15)]
    '''
    
    0 讨论(0)
提交回复
热议问题