My goal is to put a bounding box around each set of 4 subplots. I can't seem to figure out how to do it with my gridspec subplots. The examples I have came across refer to individual subplots but not to the total gridspec.
(I chose to make the plots like this using gridspec so I could have control over the spacing between the groups of subplots)
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(16,16))
fig.suptitle(' title ', fontsize=12,
bbox={'facecolor':'none', 'alpha':0.5, 'pad':5})
gs = gridspec.GridSpec(2, 2)
gs.update(top=.48,left=0.1, right=0.48, wspace=0.15,hspace=0.2)
ax1 = plt.subplot(gs[0])
ax1.set_title('axes title')
ax2 = plt.subplot(gs[1])
ax2.set_title('axes title')
ax3 = plt.subplot(gs[2])
ax3.set_title('axes title')
ax4 = plt.subplot(gs[3])
ax4.set_title('axes title')
#New Gridspec
gs1 = gridspec.GridSpec(2, 2)
gs1.update(bottom=.53,left=0.55, right=0.9, hspace=0.2, wspace=.15)
ax5 = plt.subplot(gs1[0])
ax5.set_title('axes title')
ax6 = plt.subplot(gs1[1])
ax6.set_title('axes title')
ax7 = plt.subplot(gs1[2])
ax7.set_title('axes title')
ax8 = plt.subplot(gs1[3])
ax8.set_title('axes title')
#New Gridspec
gs2 = gridspec.GridSpec(2, 2)
gs2.update(top=.48,left=0.55, right=0.9, hspace=0.2, wspace=.15)
ax9 = plt.subplot(gs2[0])
ax9.set_title('axes title')
ax10 = plt.subplot(gs2[1])
ax8.set_title('axes title')
ax11 = plt.subplot(gs2[2])
ax11.set_title('axes title')
ax12 = plt.subplot(gs2[3])
ax12.set_title('axes title')
#New Gridspec
gs3 = gridspec.GridSpec(2, 2)
gs3.update(bottom=.53,left=0.1, right=0.48, wspace=0.15,hspace=0.2)
ax13 = plt.subplot(gs3[0])
ax13.set_title('axes title')
ax14 = plt.subplot(gs3[1])
ax14.set_title('axes title')
ax15 = plt.subplot(gs3[2])
ax15.set_title('axes title')
ax16 = plt.subplot(gs3[3])
ax16.set_title('axes title')
plt.savefig('test.png',bbox_inches='tight')
To have one color bar for all subplots with Python, we can use matplotlib's subplots_adjust and colorbar methods. to call subplots_adjust on the fig subplot with the right argument to adjust the position of the subplots.
We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.
Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.
An option to create a frame or border around several subplots may be to add another axes to the figure which is a bit bigger than the extent of the respective gridspec region.
outergs = gridspec.GridSpec(1, 1)
outergs.update(bottom=.50,left=0.07, right=0.50,top=0.93)
outerax = fig.add_subplot(outergs[0])
outerax.tick_params(axis='both',which='both',bottom=0,left=0,
labelbottom=0, labelleft=0)
In order for this to work, all other axes must be created viaax_i = fig.add_subplot(gs...)
instead of ax_i = plt.subplot(gs...)
You may then of course modify the outerax
to your liking, e.g. changing its color via
outerax.set_facecolor(colors[i])
outerax.patch.set_alpha(0.3)
Complete code to reproduce the above:
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(9,9))
fig.suptitle(' title ', fontsize=12,
bbox={'facecolor':'none', 'alpha':0.5, 'pad':5})
colors=["crimson", "indigo", "limegreen", "gold"]
for i in range(4):
#outer
outergs = gridspec.GridSpec(1, 1)
outergs.update(bottom=(i//2)*.47+0.01,left=(i%2)*.5+0.02,
top=(1+i//2)*.47-0.01, right=(1+i%2)*.5-0.02)
outerax = fig.add_subplot(outergs[0])
outerax.tick_params(axis='both',which='both',bottom=0,left=0,
labelbottom=0, labelleft=0)
outerax.set_facecolor(colors[i])
outerax.patch.set_alpha(0.3)
#inner
gs = gridspec.GridSpec(2, 2)
gs.update(bottom=(i//2)*.47+0.05,left=(i%2)*.5+0.08,
top=(1+i//2)*.47-0.05, right=(1+i%2)*.5-0.05,
wspace=0.35, hspace=0.35)
for k in range(4):
ax = fig.add_subplot(gs[k])
ax.set_title('Axes Title {}'.format(k+1), color=colors[i])
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With