从数据框转换日期时间并绘制日期(x轴)与一天中的时间(y轴)

时间:2019-05-01 22:41:41

标签: datetime matplotlib time

我是Matplotlib的新手。解析.csv文件后,我需要绘制日期(x)与一天中的时间(y)的关系。

这篇文章中的代码启发了我:Matplotlib date on y axis

我可以使用以下格式解析.csv文件:

start_time,stop_time,duration
15Apr201912:23:37.99,15Apr201912:25:14.96,96.97
15Apr201914:07:01.65,15Apr201914:08:13.43,71.78
15Apr201922:13:41.32,15Apr201922:14:24.28,42.96

作为检查,打印数据框df:

                start_time               stop_time  duration
0  2019-04-15 12:23:37.990 2019-04-15 12:25:14.960     96.97
1  2019-04-15 14:07:01.650 2019-04-15 14:08:13.430     71.78

和dft:               start_time stop_time持续时间       0 12:23:37.990000 12:25:14.960000 96.97       1 14:07:01.650000 14:08:13.430000 71.78       2 22:13:41.320000 22:14:24.280000 42.96

很酷。 问题是我无法弄清楚如何将数据框中的日期和时间转换为值(我猜是mdates?),以便将它们绘制出来。我的代码返回错误:

  

TypeError:不能转换为日期时间

我的代码(对上述帖子表示感谢!)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib as mpl
import matplotlib.ticker as ticker

# test string array for date and time (this works perfectly)
date = ['3 Jan 2013', '4 Jan 2013', '5 Jan 2013', '6 Jan 2013', '7 Jan 
 2013','8 Jan 2013', '9 Jan 2013', '10 Jan 2013', '11 Jan 2013', '12 Jan 2013',
'12 Jan 2013', '14 Feb 2013', '21 Feb 2014', '7 Mar 2015', '14 Feb 2013','14 Feb 2013','14 Feb 2013','14 Feb 2013','14 Feb 2013','14 Feb 2013',
   '13 Jan 2013', '14 Jan 2013', '13 Jan 2013', '14 Jan 2013','26 Oct 2014', '17 May 2015']

 time = ['16:44:00', '16:45:00', '16:46:00', '16:47:00', '16:48:00', 
 '16:49:00','16:51:00', '16:52:00', '16:53:00', '16:55:00',
    '16:56:00', '16:57:00', '16:21:00', '16:22:00', '16:19:00', 
'16:17:00', '16:12:00', '16:10:00','16:21:00', '16:22:00',
    '16:19:00', '16:17:00', '16:12:00', 
'16:10:00','16:10:00','16:10:00']

# real task is to parse a csv file, dates and times are in atypical format - create custom date and time parser
mydateparser = lambda d: pd.datetime.strptime(d, '%d%b%Y%H:%M:%S.%f')
mytimeparser = lambda t: pd.datetime.strptime(t, '%d%b%Y%H:%M:%S.%f').time()

# parse the csv file for dates
df = pd.read_csv('somefile.txt', parse_dates= 
 ['start_time','stop_time'], date_parser=mydateparser)

# maybe I'll need the times extracted out separately, so create a separate dataframe
dft = pd.read_csv('somefile.txt', parse_dates=['start_time','stop_time'], date_parser=mytimeparser)

# post on SO said this was useful to do
dcolumns = df.index.values
tcolumns = dft.index.values

 # check the csv parsed correctly
 print(df)
 print(dft)

# statements to extract dates and times from the dataframes (they don't work...sad face)
date = pd.to_datetime(df['start_time'])
time = pd.to_datetime(dft['stop_time'])

# Convert to matplotlib's internal date format.
x = mdates.datestr2num(date)
y = mdates.datestr2num(time)

fig, ax = plt.subplots()
ax = plt.gca()
ax.plot(x, y, 'ro-')

ax.yaxis_date()
ax.xaxis_date()

ax.grid(True)
fig.autofmt_xdate()
plt.show()

感谢我对我所缺少的东西的想法。

0 个答案:

没有答案