Using GridSpec, you can create a grid of any dimension that can be used for subplot placement.
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
gs = gridspec.GridSpec(4, 4)
ax1 = plt.subplot(gs[:2, :2])
ax1.plot(range(0,10), range(0,10))
ax2 = plt.subplot(gs[:2, 2:])
ax2.plot(range(0,10), range(0,10))
ax3 = plt.subplot(gs[2:4, 1:3])
ax3.plot(range(0,10), range(0,10))
plt.show()
This will create a 4x4 grid, with each subplot taking up a 2x2 sub-grid. This allows you to properly center the bottom subplot.
To use the same same for
loop method that you use in your question, you could do the following:
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
gs = gridspec.GridSpec(4, 4)
m = 0
for i in range(0, 4, 2):
for j in range(0, 4, 2):
if m < 3:
ax = plt.subplot(gs[i:i+2, j:j+2])
ax.plot(range(0, 10), range(0, 10))
m+=1
else:
ax = plt.subplot(gs[i:i+2, 1:3])
ax.plot(range(0, 10), range(0, 10))
plt.show()
The result of both of these code snippets is:
