how to display multiple correlation heatmaps in a loop using pandas?

Solution 1:

In Jupyter notebook, you can force display an item inside a for loop with display. So in your case, you can do:

def heatmap_pandas(corr):
    mask = np.zeros_like(corr, dtype=bool)
    mask[np.tril_indices_from(mask)] = True  
    corr[mask] = np.nan

    # display the styler
    display(corr       
     .style
     .background_gradient(cmap='coolwarm', axis=None, vmin=-1, vmax=1)
     .highlight_null(null_color='#f1f1f1')  # Color NaNs grey
     .set_precision(2))

for corr in [corr_1,corr_2,corr_3]:
    heatmap_pandas(df_time_corr)