Я новичок в Matplotlib.Мне нужно построить график даты (x) и времени дня (y) после анализа файла .csv.
Я был вдохновлен кодом в этом посте: Дата Matplotlib на оси y
Я могу проанализировать файл .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 duration 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
Cool.Проблема в том, что я не могу понять, как преобразовать даты и время в кадрах данных в значения (я полагаю, mdates?), Чтобы они отображались.Мой код возвращает ошибку:
TypeError: не преобразуется в datetime
мой код (с указанием на вышеупомянутую запись!)
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()
Оценитенекоторые мысли о том, что мне не хватает.