0

I'm trying to plot a summary of many experiments in one grid of graphs in order to compare the results. My goal is to get a graph that looks as follows:

Illustration of the desired graph

I have successfully created the grid of the graphs using subfigures and the titles of rows (and even for each graph in each row). However, I could not get the X/Y-Labels to show.

The code I have been using is as follows:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (20,20) # Increasing the size of the graph to accomodate all properly.

fig = plt.figure(constrained_layout=True)
fig.suptitle(f'The big title of the set')

# Attempt 1:
plt.xlabel('The X-Label for all graphs')
ply.ylabel('The Y-Label for all graphs')

# Attempt 2:
axes = fig.gca()
axes.set_xlabel('The X-Label for all graphs', loc='left') # I was wondering if the placement was problematic.
axes.set_ylabel('The Y-Label for all graphs', visible=True) # Or maybe the visibility?

Rows = [four, parts, of, data]
Columns = [four, parts, of, values, per, row]

subfigs = fig.subfigures(nrows=len(Xs), ncols=1)
    
for row, subfig in zip(Rows, subfigs):
    subfig.suptitle(f'Title of row {row}')
    subsubfigs = subfig.subplots(nrows=1, ncols=len(Columns))
        
    for column, subsubfig in zip(Columns, subsubfigs):
        subsubfig.plot(row_data, column_data)
        subsubfig.set_title(f'Title of single graph {row}:{column}')
            
plt.show()

However, I get no labels. I do succeed adding labels to each of the subfigures using subsubfig.set_xlabel and subsubfig.set_ylabel, but I wonder why I couldn't use this on the big grid.

Kerek
  • 1,106
  • 1
  • 8
  • 18

2 Answers2

1

To set the xlabel and ylabel for the whole figure we can use supxlabel and supylabel on fig, just like you did with suptitle:

fig.supxlabel(...)
fig.supylabel(...)

I like using GridSpec since it handles like a typical matplotlib subplot where you can use set_xlabel on the subplot axes:

from matplotlib.gridspec import GridSpec
fig = plt.figure(figsize=(20, 10), constrained_layout=True)
fig.suptitle(f'The big title of the set')
gs = GridSpec(4, 1, figure=fig)
for row, subfig in ...:
    ax = fig.add_subplot(gs[row, 0])
    ax.set_xlabel(...)
    ...
            
plt.show()
Ori Yarden PhD
  • 1,287
  • 1
  • 4
  • 8
1

You can use fig.supxlabel() and fig.supylabel() for a common x and y labels:

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=4, ncols=1, figsize=(10,10))
fig.suptitle(f'The big title of the set', size=20)
fig.supxlabel('The X-Label for all graphs', x=0.5, y=0.9, size=10)
fig.supylabel('The Y-Label for all graphs', size=10)

for idx, ax in enumerate(axs):
    # There is also ax.set_title for title axis
    ax.text(x=0.5, y=0.7, s=f'Title of Row {idx}', ha='center')
    ax.text(x=0.5, y=0.5, s='Row of 4 graphs', ha='center')

plt.show()

graphs with rows

More info on the Matplotlib docs on supxlabel and supylabel.