Gilfoyle
Gilfoyle

Reputation: 3636

Matplotlib labeling sub subplots

I want to add labels to my plot which consists of sub subplots. Here is what I want (I added the outer labels with GIMP)

enter image description here

And this is what I actually get:

enter image description here

Here is the code that produces the last plot:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

plots = 16
subplots = 9

fig = plt.figure(figsize=(8, 8))

wh_plots = int(np.sqrt(plots))
wh_subplots = int(np.sqrt(subplots))

outer_grid = gridspec.GridSpec(wh_plots, wh_plots, wspace=0.1, hspace=0.1)
for p in range(plots):
    inner_grid = gridspec.GridSpecFromSubplotSpec(wh_subplots, wh_subplots, subplot_spec=outer_grid[p], wspace=0.05, hspace=0.05)
    for s in range(subplots):
        ax = plt.Subplot(fig, inner_grid[s])
        ax.imshow(np.random.rand(10,10), cmap="magma", interpolation="none")
        ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)

        if (p+1) > 12 and s == 7:
            ax.set_xlabel("sub_xlabel")
        if (p) % 4 == 0 and s == 3:
            ax.set_ylabel("sub_ylabel")

all_axes = fig.get_axes()
plt.show()

My questions:

It works, but it doesn't look right.

Upvotes: 3

Views: 300

Answers (1)

Joe
Joe

Reputation: 12417

You can add these lines before plt.show():

fig.text(0.5, 0.04, 'xlabel', ha='center', fontsize=18)
fig.text(0.04, 0.5, 'ylabel', va='center', rotation='vertical', fontsize=18)

Upvotes: 1

Related Questions