gridspec
模块,这是许多情况下非常好的工具。add_subplot()
。import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(321)
ax2 = fig.add_subplot(323)
ax3 = fig.add_subplot(325)
ax4 = fig.add_subplot(222)
ax5 = fig.add_subplot(224)
ax1
、ax2
和ax3
共享x轴,您可以使用add_subplot
的sharex
参数。可选地,关闭x轴标签应通过将它们设为不可见来完成,否则所有三个轴都将失去标签。
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(321)
ax2 = fig.add_subplot(323, sharex=ax1)
ax3 = fig.add_subplot(325, sharex=ax1)
ax4 = fig.add_subplot(222)
ax5 = fig.add_subplot(224)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
fig = plt.figure()
ax1 = plt.subplot2grid((6, 2), (0, 0), rowspan=2)
ax2 = plt.subplot2grid((6, 2), (2, 0), rowspan=2)
ax3 = plt.subplot2grid((6, 2), (4, 0), rowspan=2)
ax4 = plt.subplot2grid((6, 2), (0, 1), rowspan=3)
ax5 = plt.subplot2grid((6, 2), (3, 1), rowspan=3)