Simply loop through multi-axes figures in Matplotlib

In data visualization, especially when dealing with multiple categories or groups, it's often useful to create a grid of subplots to compare different subsets of the data. This code snippet demonstrates how to create a grid of subplots using Matplotlib and Seaborn, and then loop through each subplot to plot an ECDF for a specific metric.

fig, ax = plt.subplots(2, 2, figsize=(10,12))

for axe in ax.flat:

Here's a breakdown of what the code does:

  1. Setting Up the Subplots: plt.subplots(3, n_columns, figsize=(10,12)) creates a grid of subplots with 3 rows and n_columns columns, setting the figure size to 10 by 12 inches. The variable ax holds the axes objects for these subplots.
  2. Flattening the Axes: Since ax is a 2D array representing rows and columns, ax.flat is used to flatten this array into a 1D iterable. This makes it easier to loop through all the subplots without having to deal with nested loops.
  3. Looping Through Subplots: The loop for axe in ax.flat: iterates through each subplot (referred to as axe), and within the loop, the Seaborn sns.ecdfplot function is used to plot the ECDF for the specified metric.
  4. Plotting the ECDF: sns.ecdfplot(data=df, x='metric', ax=axe) plots the ECDF of the 'metric' column from the DataFrame df on the current subplot axe. The ECDF provides a way to visualize the distribution of the metric, showing the proportion of data points that fall below each value.

This code is useful when you want to compare the distribution of a metric across different categories or groups, and it leverages Matplotlib and Seaborn to create a visually informative grid of plots.