Matplotlib odd subplots

牧云@^-^@ 提交于 2019-11-30 21:31:04

One way of achieving what you require is to use matplotlibs subplot2grid feature. Using this you can set the total size of the grid (4,3 in your case) and choose to only plot data in certain subplots in this grid. Below is a simplified example:

import matplotlib.pyplot as plt

x = [1,2]
y = [3,4]

ax1 = plt.subplot2grid((4, 3), (0, 0))
ax2 = plt.subplot2grid((4, 3), (0, 1))
ax3 = plt.subplot2grid((4, 3), (0, 2))
ax4 = plt.subplot2grid((4, 3), (1, 0))
ax5 = plt.subplot2grid((4, 3), (1, 1))
ax6 = plt.subplot2grid((4, 3), (1, 2))
ax7 = plt.subplot2grid((4, 3), (2, 0))
ax8 = plt.subplot2grid((4, 3), (2, 1))
ax9 = plt.subplot2grid((4, 3), (2, 2))
ax10 = plt.subplot2grid((4, 3), (3, 0))
ax11 = plt.subplot2grid((4, 3), (3, 1))

plt.subplots_adjust(wspace = 0.3, hspace = 0.3) #make the figure look better

ax1.plot(x,y)
ax2.plot(x,y)
ax3.plot(x,y)
ax4.plot(x,y)
ax5.plot(x,y)
ax6.plot(x,y)
ax7.plot(x,y)
ax8.plot(x,y)
ax9.plot(x,y)
ax10.plot(x,y)
ax11.plot(x,y)

plt.show()

This produces the figure:

Just don't plot anything, but set the axis off

ax = plt.subplot(4,3,12)
ax.axis('off')

You can use a loop if necessary like this

images = [rgb, depth, depth, rgb, rgb]
fig, axes = plt.subplots(rows, columns)
    for index, axis in enumerate(axes.reshape(-1)):
        if index < no_of_images:
            axis.imshow(images[index])
        axis.axis('off')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!