2

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.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

The ticklabels cause an error (I might be missing something...) AttributeError: 'RangeIndex' object has no attribute 'strftime' All the rest works fine.
@DarkLight - Reason is not datetimeindex created by data_df.set_index('date', inplace=True)
If you want to create your datetimes on the fly you can just do something like this for your assignment to ticklabels: ticklabels = pd.Series(indexed_report_views.index).apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d'))
0

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.