我知道add_subplot()做了一个四边形网格,我正在用一个4x4网格,但是我还需要一个。我怎么能做同样的事情,但用一些奇数的情节,让它看起来像这样?

发布于 2016-12-07 19:20:34
当然,有一些非常复杂的解决方案,例如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轴,可以将sharex参数用于add_subplot。可以选择地,关闭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)发布于 2016-12-07 19:02:57
您需要使用gridspec子模块:
fig = pyplot.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=2)
ax11 = fig.add_subplot(gs[:2, 0])
ax21 = fig.add_subplot(gs[2:4, 0])
ax31 = fig.add_subplot(gs[4:, 0])
ax12 = fig.add_subplot(gs[:3, 1])
ax22 = fig.add_subplot(gs[3:, 1])
fig.tight_layout()

发布于 2016-12-07 19:05:14
潜在选项如下
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)https://stackoverflow.com/questions/41025187
复制相似问题