在使用Matplotlib的Python中,如何检查图中的子批次是否为空

在使用Matplotlib的Python中,如何检查图中的子批次是否为空,python,matplotlib,networkx,figure,subplot,Python,Matplotlib,Networkx,Figure,Subplot,我使用NetworkX创建了一些图形,并使用Matplotlib在屏幕上显示它们。具体地说,因为我事先不知道需要显示多少个图形,所以我会动态地在图上创建一个子图。那很好。但是,在脚本中的某个点上,一些子批次将从图形中删除,并且图形显示时带有一些空的子批次。我想避免它,但我无法检索图中为空的子地块。以下是我的代码: #instantiate a figure with size 12x12 fig = plt.figure(figsize=(12,12)) #when a graph is cr

我使用
NetworkX
创建了一些图形,并使用
Matplotlib
在屏幕上显示它们。具体地说,因为我事先不知道需要显示多少个图形,所以我会动态地在图上创建一个子图。那很好。但是,在脚本中的某个点上,一些
子批次
将从图形中删除,并且图形显示时带有一些空的子批次。我想避免它,但我无法检索图中为空的子地块。以下是我的代码:

#instantiate a figure with size 12x12
fig = plt.figure(figsize=(12,12))

#when a graph is created, also a subplot is created:
ax = plt.subplot(3,4,count+1)

#and the graph is drawn inside it: N.B.: pe is the graph to be shown
nx.draw(pe, positions, labels=positions, font_size=8, font_weight='bold', node_color='yellow', alpha=0.5)

#many of them are created..

#under some conditions a subplot needs to be deleted, and so..
#condition here....and then retrieve the subplot to deleted. The graph contains the id of the ax in which it is shown.
for ax in fig.axes:
    if id(ax) == G.node[shape]['idax']:
         fig.delaxes(ax)
直到这里工作正常,但当我显示该图时,结果如下所示:

您可以注意到那里有两个空的子地块。。在第二位和第五位。我怎样才能避免呢?或如何重新组织子图,使图中不再有空格


感谢您的帮助!提前感谢。

为此,我会保留一个轴列表,当我删除其中一个轴的内容时,我会将其替换为一个完整的轴。我认为下面的例子解决了这个问题(或者至少给出了解决问题的方法):


极为丑陋的for循环构造实际上只是一个占位符,用于给出如何交换轴的示例。

感谢您的提示,我将重新阐述我的脚本。我无法让
死掉
来处理matplotlib 2.1.1。然而,我发现
has_data()
方法可以作为一种变通方法。i、 e.`dead=len(my_轴)-sum([ax.has_data()表示图my_轴中的ax])
import matplotlib.pyplot as plt

# this is just a helper class to keep things clean
class MyAxis(object):
    def __init__(self,ax,fig):
        # this flag tells me if there is a plot in these axes
        self.empty = False
        self.ax = ax
        self.fig = fig
        self.pos = self.ax.get_position()

    def del_ax(self):
        # delete the axes
        self.empty = True
        self.fig.delaxes(self.ax)

    def swap(self,other):
        # swap the positions of two axes
        #
        # THIS IS THE IMPORTANT BIT!
        #
        new_pos = other.ax.get_position()
        self.ax.set_position(new_pos)
        other.ax.set_position(self.pos)
        self.pos = new_pos

def main():
    # generate a figure and 10 subplots in a grid
    fig, axes = plt.subplots(ncols=5,nrows=2)

    # get these as a list of MyAxis objects
    my_axes = [MyAxis(ax,fig) for ax in axes.ravel()]

    for ax in my_axes:
        # plot some random stuff
        ax.ax.plot(range(10))

    # delete a couple of axes
    my_axes[0].del_ax()
    my_axes[6].del_ax()

    # count how many axes are dead
    dead = sum([ax.empty for ax in my_axes])

    # swap the dead plots for full plots in a row wise fashion
    for kk in range(dead):
        for ii,ax1 in enumerate(my_axes[kk:]):
            if ax1.empty:
                print ii,"dead"
                for jj,ax2 in enumerate(my_axes[::-1][kk:]):
                    if not ax2.empty:
                        print "replace with",jj
                        ax1.swap(ax2)
                        break
                break



    plt.draw()
    plt.show()

if __name__ == "__main__":
    main()