Plot_pacf, plot_acf, autocorrelation_plot and lag_plot

Hello everyone,

I followed a tutorial to do forecasting, who uses Matplotlib , but I don’t like it. I mean, I found more beautiful Plotly, because you can interact with graphics . So, I looked the documentation to convert Matplotlib to Plotly, but I don’t how can I do it and if I can do it, as I don’t have the data using the Matplotlib functions (plot_pacf, plot_acf, autocorrelation_plot and lag_plot) . I would like to ask if someone have any idea how to do it. I checked on Github the code of those functions, but nothing.

Thanks for helps.

1 Like

Hello,

I am having the same problem as @zero94. I would like to use plotly to plot acf and pacf. I already tried to convert the plots generated by pandas library to plotly (I followed the instructions in the documentation as well), but I got three different warnings:

  1. “Blended transforms not yet supported. Zoom behavior may not work as expected.”
  2. “Bummer! Plotly can currently only draw Line2D objects from matplotlib that are in ‘data’ coordinates!”
  3. “Dang! That path collection is out of this world. I totally don’t know what to do with it yet! Plotly can only import path collections linked to ‘data’ coordinates”

Is it possible to generate this type of plots by using plotly?

Thanks in advance.

I had the same issue and my solution was as followed:

from statsmodels.tsa.stattools import pacf
import plotly.graph_objects as go
# df['sum'] is my time series where i want the pacf of.
df_pacf = pacf(df['sum'], nlags=300)
fig = go.Figure()
fig.add_trace(go.Scatter(
    x= np.arange(len(df_pacf)),
    y= df_pacf,
    name= 'PACF',
    ))
fig.update_xaxes(rangeslider_visible=True)
fig.update_layout(
    title="Partial Autocorrelation",
    xaxis_title="Lag",
    yaxis_title="Partial Autocorrelation",
    #     autosize=False,
    #     width=500,
         height=500,
    )
fig.show()

I hope this solution helps someone :slight_smile:

1 Like

All you gotta do is pass the series as a function argument and it will give you ACF plot. And add an extra argument True if you want the PACF plot.

def create_corr_plot(series, plot_pacf=False):
    corr_array = pacf(series.dropna(), alpha=0.05) if plot_pacf else acf(series.dropna(), alpha=0.05)
    lower_y = corr_array[1][:,0] - corr_array[0]
    upper_y = corr_array[1][:,1] - corr_array[0]

    fig = go.Figure()
    [fig.add_scatter(x=(x,x), y=(0,corr_array[0][x]), mode='lines',line_color='#3f3f3f') 
     for x in range(len(corr_array[0]))]
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=corr_array[0], mode='markers', marker_color='#1f77b4',
                   marker_size=12)
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=upper_y, mode='lines', line_color='rgba(255,255,255,0)')
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=lower_y, mode='lines',fillcolor='rgba(32, 146, 230,0.3)',
            fill='tonexty', line_color='rgba(255,255,255,0)')
    fig.update_traces(showlegend=False)
    fig.update_xaxes(range=[-1,42])
    fig.update_yaxes(zerolinecolor='#000000')
    
    title='Partial Autocorrelation (PACF)' if plot_pacf else 'Autocorrelation (ACF)'
    fig.update_layout(title=title)
    fig.show()

The function assumes you have imported the necessary functions (not the plot ones, the array ones) from statsmodels library.

3 Likes

This is wonderful, thank you I was using it. I was playing with it and attempted to add nlags

def create_corr_plot(series, plot_pacf=False, nlags=None):
    corr_array = sm.tsa.pacf(series.dropna(), alpha=0.05, nlags=nlags) if plot_pacf else sm.tsa.acf(series.dropna(), alpha=0.05, nlags=nlags)
    lower_y = corr_array[1][:,0] - corr_array[0]
    upper_y = corr_array[1][:,1] - corr_array[0]

    fig = go.Figure()
    [fig.add_scatter(x=(x,x), y=(0,corr_array[0][x]), mode='lines',line_color='#3f3f3f') 
     for x in range(len(corr_array[0]))]
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=corr_array[0], mode='markers', marker_color='#1f77b4',
                   marker_size=12)
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=upper_y, mode='lines', line_color='rgba(255,255,255,0)')
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=lower_y, mode='lines',fillcolor='rgba(32, 146, 230,0.3)',
            fill='tonexty', line_color='rgba(255,255,255,0)')
    fig.update_traces(showlegend=False)
    fig.update_xaxes(range=[-1, nlags + 1])
    fig.update_yaxes(zerolinecolor='#000000')
    
    title='Partial Autocorrelation (PACF)' if plot_pacf else 'Autocorrelation (ACF)'
    fig.update_layout(title=title)
    fig.show()