Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: 3 plots plotted in 2 rows with single image centered

I have a 3 figure plot. I would like to have two rows of images with 2 plots on the top row, and 1 on the bottom. I need to center align the single plot on the second row.

fig, ax = plt.subplots(nrows=2, ncols=2)
x = np.arange(0.01,1.01,0.01)
y = np.arange(0.01,1.01,0.01)
X,Y = np.meshgrid(x, y) # grid of point
Z = 1/((1/X)+(1/Y)-1)
F = Y / X
values = [0.3, 0.5, 0.8, 1.0, 1.3, 1.5, 2.0, 3.0, 5.0, 10.0]

#Plot 0
ax[0,0].set_aspect('equal')
CS = ax[0,0].contour(X,Y,Z,np.arange(0.1,1.0,0.1),colors='black',linewidths=0.7)
ax[0,0].clabel(CS, fontsize=12, fmt='%1.1f', inline=1)
CS = ax[0,0].contour(X,Y,F,values,colors='blue', linewidths=0.7)
ax[0,0].clabel(CS, fontsize=12, fmt='%1.1f', inline=1)

#Plot 1
ax[0,1].set_aspect('equal')
CS = ax[0,1].contour(X,Y,Z,np.arange(0.1,1.0,0.1),colors='black',linewidths=0.7)
ax[0,1].clabel(CS, fontsize=12, fmt='%1.1f', inline=1)
CS = ax[0,1].contour(X,Y,F,values,colors='blue', linewidths=0.7)
ax[0,1].clabel(CS, fontsize=12, fmt='%1.1f', inline=1)

#Plot 2
ax[1,0].set_aspect('equal')
CS = ax[1,0].contour(X,Y,Z,np.arange(0.1,1.0,0.1),colors='black',linewidths=0.7)
ax[1,0].clabel(CS, fontsize=12, fmt='%1.1f', inline=1)
CS = ax[1,0].contour(X,Y,F,values,colors='blue', linewidths=0.7)
ax[1,0].clabel(CS, fontsize=12, fmt='%1.1f', inline=1)

For example, plot 2 is in the second row and I would like it centered in the second row but to be the same size as the other 2 plots.

Thanks in advance

like image 904
bgame2498 Avatar asked Mar 14 '16 17:03

bgame2498


1 Answers

You can use gridspec.Gridspec(). The trick here is to create a regular grid but not to have a plot in every cell. For example, in this case, I created a 2x4-cell grid. Each plot spans 2 cells. So on the first row, I have 2 plots (2x2 cells). On the second row, I have one empty cell, 1 plot (1x2 cells) and another empty cell.

import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 4)
gs.update(wspace=0.5)
ax1 = plt.subplot(gs[0, :2], )
ax2 = plt.subplot(gs[0, 2:])
ax3 = plt.subplot(gs[1, 1:3])
plt.show()

enter image description here

like image 132
Julien Spronck Avatar answered Oct 04 '22 16:10

Julien Spronck