Python Forum

Full Version: How to plot date series in matplotlib?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
Please post the error code in it's entirety and the code relevant to the error being thrown.
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:
Error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-79-7400273c000e> in <module>() 2 import random 3 ----> 4 x = matplotlib.dates.date2num(spreads['Created at']) 5 y = [i+random.gauss(0,1) for i,_ in enumerate(x)] 6 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in date2num(d) 360 if not d.size: 361 return d --> 362 return _to_ordinalf_np_vectorized(d) 363 364 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs) 2732 vargs.extend([kwargs[_n] for _n in names]) 2733 -> 2734 return self._vectorize_call(func=func, args=vargs) 2735 2736 def _get_ufunc_and_otypes(self, func, args): /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args) 2802 res = func() 2803 else: -> 2804 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args) 2805 2806 # Convert args to object arrays first /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args) 2762 2763 inputs = [arg.flat[0] for arg in args] -> 2764 outputs = func(*inputs) 2765 2766 # Performance note: profiling indicates that -- for simple /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/dates.py in _to_ordinalf(dt) 218 tzi = UTC 219 --> 220 base = float(dt.toordinal()) 221 222 # If it's sufficiently datetime-like, it will have a `date()` method AttributeError: 'str' object has no attribute 'toordinal'