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

3

5

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!

Birdwell answered 30/8, 2016 at 12:55 Comment(2)
Try setting the 'Date' column as the index with df.set_index (...)Debera
Or put df as first argument then specify the time and value kwargs accordingly.Debera
E
9

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

df[['Date','Value']].set_index('Date').plot()
Eyecatching answered 30/8, 2016 at 14:47 Comment(2)
this is simple and working, and yet one may want to note that if not all consecutive dates exist in the data, this will not generate an axis continuous/linear to the passing of time.Molli
One could use the resample() to get a continuous axis.Banner
S
2

use the time parameter for tsplot

from docs:

time : string or series-like
    Either the name of the field corresponding to time in the data DataFrame or x values for a plot when data is an array. If a Series, the name will be used to label the x axis.
#Plot the Value column against Date column
sns.tsplot(data = df['Value'], time = df['Date'])

However tsplot is used to plot timeseries in the same time window for different conditions. To plot a single timeseries you could also use plt.plot(time = df['Date'], data = df['Value'])

Shading answered 16/9, 2016 at 14:53 Comment(0)
T
2

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'])
Thoreau answered 21/11, 2017 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.