How to plot dates on the x-axis using Seaborn (or matplotlib)

equanimity picture equanimity · Aug 30, 2016 · Viewed 36.5k times · Source

I have a csv file with some time series data. I create a Data Frame as such:

df = pd.read_csv('C:\\Desktop\\Scripts\\TimeSeries.log')

When I call df.head(6), the data appears as follows:

Company     Date                 Value
ABC         08/21/16 00:00:00    500
ABC         08/22/16 00:00:00    600
ABC         08/23/16 00:00:00    650
ABC         08/24/16 00:00:00    625
ABC         08/25/16 00:00:00    675
ABC         08/26/16 00:00:00    680

Then, I have the following to force the 'Date' column into datetime format:

df['Date'] = pd.to_datetime(df['Date'], errors = 'coerce')

Interestingly, I see "pandas.core.series.Series" when I call the following:

type(df['Date'])

Finally, I call the following to create a plot:

%matplotlib qt
sns.tsplot(df['Value'])

On the x-axis from left to right, I see integers ranging from 0 to the number of rows in the data frame. How does one add the 'Date' column as the x-axis values to this plot?

Thanks!

Answer

milos.ai picture milos.ai · Aug 30, 2016

Not sure that tsplot is the best tool for that. You can just use:

df[['Date','Value']].set_index('Date').plot()