Spiros
Spiros

Reputation: 2336

Shared axis with gridspec subplots

I'm using nested GridSpecFromSubplotSpec to create a nested grid of axes. I have two independent set of axes, a top one and a bottom one. Each set has four axes, arranged in a 2x2 grid.

Here is the code I'm using and the result I obtain:

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

fig = plt.figure()
global_gsp = gsp.GridSpec(2, 1)

for i in range(2):
    axes = np.empty(shape=(2, 2), dtype=object)
    local_gsp = gsp.GridSpecFromSubplotSpec(2, 2, subplot_spec=global_gsp[i])

    for j in range(2):
        for k in range(2):
            ax = plt.Subplot(fig, local_gsp[j, k],
                             sharex=axes[0, 0], sharey=axes[0, 0])
            fig.add_subplot(ax)
            axes[j, k] = ax

    for j in range(2):
        for k in range(2):
            ax = axes[j, k]
            x = i + np.r_[0:1:11j]
            y = 10*i + np.random.random(11)
            ax.plot(x, y, color=f'C{i}')
            ax.set_xlabel('x')
            ax.set_ylabel('y')


plt.show()

Resulting figure

As you can see, the top set has blue lines, the bottom set has orange lines, and the blue lines are well represented with the limits [0, 1]x[0, 1], while the orange lines are represented with the limits [1, 2]x[10, 11]. When I create the subplots with plt.Subplot, I use the sharex and sharey arguments to have exactly the same scale on all four axes in each set (but different scale across different sets).

I would like to aviod the repetition of the label and the ticks of each axis. How can I achieve that?

Upvotes: 4

Views: 5965

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

Subplot axes have functions is_{first,last}_{col,row}() (although I could not find the documentation anywhere) as shown in this matplotlib tutorial. These functions are useful to only print the label(s) and/or ticks in the right spot. To hide the tick labels, shared_axis_demo.py recommends using setp(ax.get_{x,y}ticklabels(), visible=False)

fig = plt.figure()
global_gsp = gs.GridSpec(2, 1)

for i in range(2):
    axes = np.empty(shape=(2, 2), dtype=object)
    local_gsp = gs.GridSpecFromSubplotSpec(2, 2, subplot_spec=global_gsp[i])

    for j in range(2):
        for k in range(2):
            ax = plt.Subplot(fig, local_gsp[j, k],
                             sharex=axes[0, 0], sharey=axes[0, 0])
            fig.add_subplot(ax)
            axes[j, k] = ax

    for j in range(2):
        for k in range(2):
            ax = axes[j, k]
            x = i + np.r_[0:1:11j]
            y = 10*i + np.random.random(11)
            ax.plot(x, y, color=f'C{i}')


            #
            # adjust axes and tick labels here
            #
            if ax.is_last_row():
                ax.set_xlabel('x')
            else:
                plt.setp(ax.get_xticklabels(), visible=False)

            if ax.is_first_col():
                ax.set_ylabel('y')
            else:
                plt.setp(ax.get_yticklabels(), visible=False)


fig.tight_layout()
plt.show()

enter image description here

Upvotes: 7

Related Questions