I'm having some intermittent issues sharing a single, centered xlabel across three subplots that 1) only span part of a gridspec row and 2) whose width relative to one another may vary.
Using the docs I've been able to sort-out the basic multiplot structure I'm looking for:
fig = plt.figure(figsize=(10,8), constrained_layout=True)
xlabel_234 = 'XLabel Thing2\n(to be centered under ax2, ax3, and ax4 with equal horiz. spacing btwn subplots)'
gs = fig.add_gridspec(nrows=14, ncols=1)
gs0 = gs[0:2]
gs1 = gs[2:].subgridspec(nrows=1, ncols=12)
ax1 = fig.add_subplot(gs0)
ax1.set_title('Something Short and Wide')
ax1.text(0.5, 0.5, 'ax1', ha='center')
ax1.set_xlabel('XLabel Thing1')
ax2 = fig.add_subplot(gs1[0, 0:1])
ax2.set_title('Something Tall\nand Narrow 2a')
ax2.text(0.5, 0.5, 'ax2', ha='center')
ax3 = fig.add_subplot(gs1[0, 1:6], sharey=ax2)
ax3.set_title('Something Tall\nand Narrow 2b')
ax3.text(0.5, 0.5, 'ax3', ha='center')
plt.setp(ax3.get_yticklabels(), visible=False)
ax3.set_xlabel(xlabel_234, x=0.7) # *x offset is a bit of hack*
ax4 = fig.add_subplot(gs1[0, 6:9], sharey=ax2)
ax4.set_title('Something Tall\n and Narrow 2c')
ax4.text(0.5, 0.5, 'ax4', ha='center')
plt.setp(ax4.get_yticklabels(), visible=False)
ax5 = fig.add_subplot(gs1[0, 9:12])
ax5.set_title('Something Tall\nand Narrow 3')
ax5.text(0.5, 0.5, 'ax5', ha='center')
ax5.set_xlabel('XLabel Thing3')
I want to recognize this post for helping me sort-out the y-axis sharing. the visibility kwarg in setp is a huge help.
This post helped me (kinda) get a common xlabel centered, but it's a bit of a hack and can produce some odd behaviour (that affects spacing) depending on the relative widths of ax2, ax3, ax4. And I have to iteratively guess at the x offset value.
Is there a more precise way for me to do this? And, perhaps, standardize the xtick label formats (I know how to do them one-off). Like maybe a subgridspec-of-the-subgridspec? Alternatively, other docs show a 'width_ratios' kwarg (though there is not much info on implementing)...would this be a better approach (i.e. where subplot spacing might not be as sensitive)? Other?
Cheers