如何在Python中设置一个图中的子图
我想把我的子图在图形中对齐,具体想要这样排版:
使用这段代码:
fig = plt.figure()
ax1 = fig.add_subplot(131)
a = ax1.imshow(im,interpolation='none',origin='lower')
ax2 = fig.add_subplot(132)
b = ax2.imshow(best_fit,origin='lower')
ax3 = fig.add_subplot(133)
c = ax3.imshow(residual,interpolation='none',origin='lower')
ax4 = fig.add_subplot(123)
d = ax3.imshow(correlation_mat,interpolation='none',origin='lower',vmin=-1,vmax=1)
我想创建一个图形,让图 a、b 和 c 都在左边的一列里,然后图 d 占据右边大部分的空间。我知道这个应该在
fig.add_subplot()
那一行里说明,但我不太确定该怎么做。
1 个回答
4
subplot2grid 是一个很不错的功能,可以用来创建更复杂的布局。你可以先定义一个网格,然后在这个网格的特定位置添加坐标轴。在这个例子中,我用了3列和3行。你还可以定义坐标轴的行跨度和列跨度,这样就能让一个坐标轴覆盖多个网格位置。
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = plt.subplot2grid((3,3), (0,0))
ax2 = plt.subplot2grid((3,3), (1,0))
ax3 = plt.subplot2grid((3,3), (2,0))
ax3 = plt.subplot2grid((3,3), (0,1), rowspan=3, colspan=2)
fig.tight_layout()
结果看起来是这样的: