How to plot date series in matplotlib? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How to plot date series in matplotlib? (/thread-7783.html) |
How to plot date series in matplotlib? - StrybolData - Jan-24-2018 I used the following code to format my series into timestamps, but I would like to now visualise it with pyplot. I based the format on the python datetime object, so to plot time with matplotlib I converted my column into a python datetime and then to the pyplot format... Datetime format spreads['Created at'] = pd.to_datetime(timedata).apply(lambda x:x.strftime('%H:%M:%S'))outcome : 07:20:00 Next step pyplot format > import matplotlib.pyplot as plt import random x = matplotlib.dates.date2num(spreads['Created at']) y = y = [i+random.gauss(0,1) for i,_ in enumerate(x)] plt.plot(x,y)AttributeError: 'str' object has no attribute 'toordinal' Could somebody explain me what is wrong with my code? RE: How to plot date series in matplotlib? - sparkz_alot - Jan-25-2018 Please post the error code in it's entirety and the code relevant to the error being thrown. RE: How to plot date series in matplotlib? - StrybolData - Jan-25-2018 Ok the entire code: import pandas as pd spreads= pd.read_csv('data.csv',sep=',',encoding ="Latin-1") spreads['Created at'] = pd.to_datetime(timedata, errors='coerce').apply(lambda x:x.strftime('%H:%M:%S')) import matplotlib.pyplot as plt import random x = matplotlib.dates.date2num(spreads['Created at']) y = [i+random.gauss(0,1) for i,_ in enumerate(x)] plt.plot(x,y)The data of the csv is printed like this: 0 07:20:00 1 07:20:00 2 07:20:00 3 07:20:00 4 07:20:00 5 07:35:00 6 08:42:00 7 12:28:00 The error:
|