0
是否可以創建python matplotlib散點圖,其中x軸是日期,y軸是時間。我希望能夠使用下面的兩個單獨的列表。使用列表在matplotlib散點圖上繪製日期和時間
dates = ['2015-12-20','2015-09-12']
times = ['22:50:12','19:33:09']
是否可以創建python matplotlib散點圖,其中x軸是日期,y軸是時間。我希望能夠使用下面的兩個單獨的列表。使用列表在matplotlib散點圖上繪製日期和時間
dates = ['2015-12-20','2015-09-12']
times = ['22:50:12','19:33:09']
這使用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()