Python 使用“将子地块添加到特定地物”;子地块2Grid“;方法

Python 使用“将子地块添加到特定地物”;子地块2Grid“;方法,python,matplotlib,python-3.6,Python,Matplotlib,Python 3.6,我正在尝试向特定matplotlib图形添加不同大小的子图,但不确定如何添加。在只有一个图形的情况下,“subplot2grid”可按如下方式使用: import matplotlib.pyplot as plt fig = plt.figure() ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2) ax1 = plt.subplot2grid((2, 2), (1, 1)) plt.show() 上面的代码创建一个地物,并向该地物添加两

我正在尝试向特定matplotlib图形添加不同大小的子图,但不确定如何添加。在只有一个图形的情况下,“subplot2grid”可按如下方式使用:

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
ax1 = plt.subplot2grid((2, 2), (1, 1))

plt.show()
上面的代码创建一个地物,并向该地物添加两个子地块,每个地块的尺寸不同。现在,我的问题出现在有多个图形的情况下——我无法找到使用“subplot2grid”将子图添加到特定图形的适当方法。使用更简单的“add_subplot”方法,可以将子图添加到特定图形,如以下代码所示:

import matplotlib.pyplot as plt

fig1 = plt.figure()
fig2 = plt.figure()

ax1 = fig1.add_subplot(2, 2, 1)
ax2 = fig1.add_subplot(2, 2, 4)

plt.show()
我正在寻找类似的方法,将不同大小的子图添加到特定图形中(最好使用某种网格管理器,例如“subplot2grid”)。我对使用plt。“x”样式有所保留,因为它在创建的最后一个图形上运行--我的代码将有几个图形,所有这些我都需要有不同尺寸的子批次

提前感谢,

Curtis M.

在未来(可能是即将发布的版本?), 将采用
fig
参数

subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)
使以下各项成为可能:

import matplotlib.pyplot as plt

fig1=plt.figure()
fig2=plt.figure()

ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2, fig=fig1)
ax2 = plt.subplot2grid((2, 2), (1, 1),  fig=fig1)

plt.show()
到目前为止(版本2.0.2),这还不可能。或者,您可以手动定义基础GridSpec

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig1=plt.figure()
fig2=plt.figure()

spec1 = GridSpec(2, 2).new_subplotspec((0,0), colspan=2)
ax1 = fig1.add_subplot(spec1)
spec2 = GridSpec(2, 2).new_subplotspec((1,1))
ax2 = fig1.add_subplot(spec2)

plt.show()
或者,您可以简单地设置当前图形,以便
plt.subplot2grid
可以处理该精确图形(如中所示)


您可以设置当前图形,然后您的plt。“x”样式命令将转到您选择的任何图形。GridSpec的手动定义工作得很好。非常感谢。
import matplotlib.pyplot as plt

fig1=plt.figure(1)
fig2=plt.figure(2)

# ... some other stuff

plt.figure(1) # set current figure to fig1
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
ax2 = plt.subplot2grid((2, 2), (1, 1))

plt.show()