✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

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.


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()
    x= np.arange(len(df_pacf)),
    y= df_pacf,
    name= 'PACF',
    title="Partial Autocorrelation",
    yaxis_title="Partial Autocorrelation",
    #     autosize=False,
    #     width=500,

I hope this solution helps someone :slight_smile:

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',
    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)')
    title='Partial Autocorrelation (PACF)' if plot_pacf else 'Autocorrelation (ACF)'

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