Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 407
equanimity Avatar asked Dec 15 '22 04:12

equanimity


2 Answers

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

df[['Date','Value']].set_index('Date').plot()
like image 154
milos.ai Avatar answered Jan 28 '23 02:01

milos.ai


I think it is too late.

First, you have to notice that 'Date' column is a series of 'datetime' type so you should do that to get the 'date' part:

df['Date'] = df['Date'].map(lambda x:x.date())

now group your data frame by 'Date' and then reset index in order to make 'Date' a column (not an index).

Then you can use plt.plot_date

df_groupedby_date = df.groupby('Date').count()
df_groupedby_date.reset_index(inplace=True)
plt.plot_date(x=df_groupedby_date['Date'], y=df_groupedby_date['Value'])
like image 34
Mohanad Refaai Avatar answered Jan 28 '23 02:01

Mohanad Refaai