Python Forum
How to plot date series in matplotlib?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to plot date series in matplotlib?
#1
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?
Reply
#2
Please post the error code in it's entirety and the code relevant to the error being thrown.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fit straight line to pandas time series data with semilog plot schniefen 2 1,502 Mar-10-2023, 01:08 PM
Last Post: jefsummers
  Plot time series data schniefen 3 1,271 Mar-04-2023, 04:22 PM
Last Post: noisefloor
  matplotlib : Raster Plot adithyakrish 1 3,663 May-28-2019, 05:04 AM
Last Post: heiner55
  Need to convert a date and price from a list into appropriate date format & currency SilverLeaf90 1 1,942 Mar-23-2019, 09:41 PM
Last Post: Yoriz
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 8,124 Nov-10-2018, 01:32 AM
Last Post: wlsa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020