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
  How do I make my plot show up? ThomasM4 0 117 Dec-09-2018, 05:28 AM
Last Post: ThomasM4
  Tips on surface plot HW question Cwcox 1 180 Nov-14-2018, 11:45 PM
Last Post: Larz60+
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 354 Nov-10-2018, 01:32 AM
Last Post: wlsa
  How to create custom error bars in matplotlib.pyplot? wlsa 1 221 Nov-04-2018, 09:49 PM
Last Post: wlsa
  Adding text to plot Pythcoronas 1 255 Sep-13-2018, 12:27 PM
Last Post: baby_quant
  plot the mean in a bar diagram sussii 3 386 May-23-2018, 11:27 PM
Last Post: wavic
  Help needed on Fibonacci series nvakada 1 417 Apr-25-2018, 07:12 PM
Last Post: micseydel
  Write a program to compute the sum of the terms of the series: 4 - 8 + 12 - 16 + 20 - chewey777 0 533 Mar-24-2018, 12:39 AM
Last Post: chewey777
  creating date/time stamp from dataframe values kiki1113 1 503 Dec-06-2017, 05:43 PM
Last Post: gruntfutuk
  summation of series student8 6 1,231 Oct-15-2017, 06:13 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)