Using Pandas Autocorrelation Plot - how to limit x-axis to make it more readable?
Asked Answered
A

3

11

I am using python 3.7.

I am performing time series forecasting using an ARIMA model. I am assessing the properties of my data for ARIMA using an Autocorrelation Plot - specifically using autocorrelation_plot from pandas.plotting.

My data has 50,000 records or so, making the plot extremely busy and hard to pick out any specific trends. Is there a way to limit the x-axis to bring the first few hundred lags more into focus?

I can't share the actual plot, but my code is as follow:

import pandas as pd
from pandas.plotting import autocorrelation_plot

#Import Data
time_series_2619 = pd.read_csv("Consumption/2619.csv", parse_dates=['Date/Time'], index_col = ['Date/Time'])['Recording']

#Auto Correlation Plot
autocorrelation_plot(time_series_2619)

I couldn't find anything in the documentation.

Apodosis answered 11/4, 2019 at 9:7 Comment(0)
C
14

autocorrelation_plot returns a matplotlib.axis object. Hence, you can simply use the set_xlim() method to limit the x-axis:

ax = autocorrelation_plot(time_series_2619)
ax.set_xlim([0, 500])
Cattycornered answered 11/4, 2019 at 9:14 Comment(0)
B
5

Alternatively, you can use the plot_acf() function and specify the lags.

# import the plotting functions for act and pacf  
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plot_acf(df1['Thousands of Passengers'], lags=40);
Bhang answered 3/5, 2019 at 19:19 Comment(1)
Heavily underrated, especially if your time series is large enough.Silma
C
2

Just to add another way of plotting the autocorrelation, which to be honest is much faster in small-data cases:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
plt.title('Autocorrelation plot')
plt.plot(np.arange(720), [time_series_2619['column_name'].autocorr(j) for j in range(720)])
plt.show();

You are just using the Series.autocorr() function of a pandas series, which needs a lag number and returns the autocorrelation between the two timestamps. Doing a simple comprehension list you will be able to have an array of autocorrelations, which can be plotted easily using pyplot.

Cousingerman answered 17/2, 2020 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.