jeangelj
jeangelj

Reputation: 4498

python pandas time line graph

I have the following data frame

data_df = 
date          value
2016-01-15    1555
2016-01-16    1678
2016-01-17    1789
...  

I would like to create a timeline graph, with the date as the x axis

I import the visualization modules

import matplotlib.pyplot as plt
%matplotlib inline
import vincent as vin
import seaborn as sb

I try to add a column to format the date data_df['dates'] = plt.date2num(ad_data.date)

Then I want to plot the timeline plot_date(data_df.dates, data_df.shown)

This doesn't work, since I am not converting the date correctly.

Upvotes: 2

Views: 11100

Answers (2)

jezrael
jezrael

Reputation: 863541

You can use:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

#sample data    
start = pd.to_datetime('2016-01-15')
rng = pd.date_range(start, periods=100)

data_df = pd.DataFrame({'date': rng, 'value': range(100)})  
data_df.value = data_df.value * 15 / data_df.date.dt.day
print (data_df)
         date        value
0  2016-01-15     0.000000
1  2016-01-16     0.937500
2  2016-01-17     1.764706
3  2016-01-18     2.500000
4  2016-01-19     3.157895
5  2016-01-20     3.750000
6  2016-01-21     4.285714
7  2016-01-22     4.772727
8  2016-01-23     5.217391
9  2016-01-24     5.625000
10 2016-01-25     6.000000
...
...

If necessary convert column date to to_datetime and then set_index from column date:

data_df.date = pd.to_datetime(data_df.date)
data_df.set_index('date', inplace=True)
print (data_df)
                  value
date                   
2016-01-15     0.000000
2016-01-16     0.937500
2016-01-17     1.764706
2016-01-18     2.500000
2016-01-19     3.157895
2016-01-20     3.750000
2016-01-21     4.285714
2016-01-22     4.772727
2016-01-23     5.217391
2016-01-24     5.625000
2016-01-25     6.000000
...
...

Plot Series data_df['value'] by plot and then set format of axis x:

ax = data_df['value'].plot()

ticklabels = data_df.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.show()

graph

Upvotes: 5

Mathias711
Mathias711

Reputation: 6668

If your date is a datetime thingy (if not, use pd.to_datetime(), it should recognize the format), it should work by just calling date_df.plot(). Make sure it is set as index (so use date_df.index = date_df['date']

Upvotes: 0

Related Questions