0

I have a date array

date_array = array(['September 11, 2012', 'September 5, 2012'])

I want to graph this date_array against a value, value_array =array([25,28]) using matplotlib

So I want to convert the date_array to a number like

date_as_number=array([20120812, 20120805])

so that I could plot the date_as_number as any other number against the value_array.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
Sanjay
  • 169
  • 2
  • 9
  • What about https://stackoverflow.com/questions/23644020/matplotlib-string-to-dates ? Please make it clear in how far other questions here do not solve your problem. – ImportanceOfBeingErnest Feb 23 '18 at 10:47

2 Answers2

0

Set the date column as index and use the pandas builtin plot to get the date wise plot that you need. Below is an example

import random
import pandas as pd
data = [('September {}, 2012'.format(i),random.random()) for i in range(1,30)]
data_df = pd.DataFrame(data,columns=['_date','value'])
data_df['_date'] = pd.to_datetime(data_df._date)
data_df= data_df.set_index('_date')
data_df.head()

This gives a dataframe like this

            value
_date   
2012-09-01  0.379791
2012-09-02  0.146355
2012-09-03  0.545267
2012-09-04  0.398388
2012-09-05  0.926643

Just plot this df.

 data_df.plot()

enter image description here

vumaasha
  • 2,765
  • 4
  • 27
  • 41
0

To Parse Dates

from dateutil.parser import parse

date_array = np.array(['September 11, 2012', 'September 5, 2012'])
[parse(x).date() for x in date_array]

Output:

[datetime.date(2012, 9, 11), datetime.date(2012, 9, 5)]

Rayadurai
  • 66
  • 6