下面的代码会在子图之间产生间隙。如何消除子图之间的间隙并使图像成为紧密的网格?
import matplotlib.pyplot as plt for i in range(16): i = i + 1 ax1 = plt.subplot(4, 4, i) plt.axis('on') ax1.set_xticklabels([]) ax1.set_yticklabels([]) ax1.set_aspect('equal') plt.subplots_adjust(wspace=None, hspace=None) plt.show()
您可以使用gridspec来控制轴之间的间距。这里有更多信息。
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec plt.figure(figsize = (4,4)) gs1 = gridspec.GridSpec(4, 4) gs1.update(wspace=0.025, hspace=0.05) # set the spacing between axes. for i in range(16): # i = i + 1 # grid spec indexes from 0 ax1 = plt.subplot(gs1[i]) plt.axis('on') ax1.set_xticklabels([]) ax1.set_yticklabels([]) ax1.set_aspect('equal') plt.show()