Bottom Page

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?
Quote
#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
Quote
#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'
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  matplotlib : Raster Plot adithyakrish 1 1,297 May-28-2019, 05:04 AM
Last Post: heiner55
  How to create a new plot in a figure right below the first one after click event? codexx 1 110 May-26-2019, 12:52 PM
Last Post: heiner55
  operation with series elmismo999 1 106 May-25-2019, 11:33 AM
Last Post: ichabod801
  Plot a function dxfrelince 3 124 May-21-2019, 12:23 PM
Last Post: DeaD_EyE
  Issues with matplotlib.pyplot schniefen 2 172 Apr-19-2019, 11:51 AM
Last Post: schniefen
  Need to convert a date and price from a list into appropriate date format & currency SilverLeaf90 1 154 Mar-23-2019, 09:41 PM
Last Post: Yoriz
  How do I make my plot show up? ThomasM4 0 305 Dec-09-2018, 05:28 AM
Last Post: ThomasM4
  Tips on surface plot HW question Cwcox 1 370 Nov-14-2018, 11:45 PM
Last Post: Larz60+
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 851 Nov-10-2018, 01:32 AM
Last Post: wlsa
  How to create custom error bars in matplotlib.pyplot? wlsa 1 582 Nov-04-2018, 09:49 PM
Last Post: wlsa

Forum Jump:


Users browsing this thread: 1 Guest(s)