Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable wspace with gridspec.GridSpec in python

I would like to make a variable (two different) wspace using GridSpec in matplotlib.

I would like to achieve the following: Goal

I'm using the following so far:

gs1 = gridspec.GridSpec(6, 3, width_ratios=[1.5,1,1])
gs1.update(wspace=0.4, hspace=0.3)
ax1 = fig.add_subplot(gs1[0:2,0])
ax2 = fig.add_subplot(gs1[2:4,0])
ax3 = fig.add_subplot(gs1[4:6,0])
ax4 = fig.add_subplot(gs1[0:3,1])
ax5 = fig.add_subplot(gs1[3:6,1])
ax6 = fig.add_subplot(gs1[0:3,2])
ax7 = fig.add_subplot(gs1[3:6,2])

Any idea how to obtain the two different space highlighted in green in my amazing hand drawing ?

Thanks a lot !

Sam

like image 404
sponce Avatar asked Sep 05 '25 13:09

sponce


1 Answers

You may use 2 GridSpecs, one which contains one column and 3 rows and one which contains two rows and two colums. You can then let the first extend only to less than half of the figure and start the second at half the figure width. The difference between the left and right parameter will be the spacing.

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs1 = GridSpec(3, 1, right=0.4)
gs2 = GridSpec(2, 2, left=0.5)


ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])

plt.show()

enter image description here

The same can be achieved with first defining an "outer" gridspec with two columns and placing an inner gridspec into each of them.

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec

fig = plt.figure()
gs = GridSpec(1, 2, width_ratios=[1.5,2], wspace=0.3)

gs1 = GridSpecFromSubplotSpec(3, 1, subplot_spec=gs[0])
gs2 = GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])

ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])

plt.show()
like image 146
ImportanceOfBeingErnest Avatar answered Sep 10 '25 05:09

ImportanceOfBeingErnest