2015-11-22 79 views

回答

0

這使用plot_date,而不是scatter。它可能會給你你需要的東西。

import datetime 
from matplotlib import pyplot as plt 
from matplotlib.dates import date2num 


def date_to_days(date): 
    return date2num(datetime.datetime.strptime(date, '%Y-%m-%d')) 


def time_to_hours(time): 
    [hh, mm, ss] = [int(x) for x in time.split(':')] 
    seconds = datetime.timedelta(hours=hh, minutes=mm, seconds=ss).seconds 
    hours = seconds/float(3600) 
    return hours 


if __name__ == '__main__': 
    start_date = '2015-08-01' 
    end_date = '2015-12-31' 

    dates = ['2015-12-20','2015-09-12'] 
    times = ['22:50:12','19:33:09'] 

    days = [date_to_days(d) for d in dates] 
    hours = [time_to_hours(t) for t in times] 

    plt.plot_date(days, hours, ydate=False) 
    plt.axis([date_to_days(start_date), date_to_days(end_date), 0, 24]) 
    plt.xlabel('Date') 
    plt.ylabel('Time (hours)') 
    plt.show() 
相關問題