Looping through a list of dataframes to create different plots

Viewed 92

I have a list of dataframes:

list=[mean_ave,rmse_ave,bias_ave,
                std_ave,std_diff_ave,trend_ave,trend_diff_ave,
                corr_mean,ano_rmsd]

For each of those dataframes I would like to produce a heatmap as shown in the example below.

f, ax = plt.subplots(figsize=(8, 12))
sns.heatmap(mean_ave, cmap='RdBu', 
            annot=True, 
            fmt='.2f')

I was thinking of looping through the list but I am quite inexperienced.

Help would be greatly appreciated.

2 Answers

The key is to add the ax parameter in the heatmap

for idx, df in enumerate(list):
    f, ax = plt.subplots(len(df),1 , figsize=(8, 12))
    sns.heatmap(df, cmap='RdBu', 
                annot=True, 
                fmt='.2f', ax=ax[idx])

One remark, first: list is not the best choice of name, use df_list or dfs, for instance (or any other name less ambiguous).

You could try this:

f, axs = plt.subplots(len(df_list), 1, figsize=(8, 12))
for i, df in enumerate(list_df):
    sns.heatmap(
    df,
    cmap='RdBu', 
    annot=True, 
    fmt='.2f',
    ax=axs[i])
Related