【发布时间】:2014-05-20 21:43:38
【问题描述】:
我有一些使用NetworkX 创建的图表,并使用Matplotlib 在屏幕上显示它们。具体来说,由于我事先不知道需要显示多少个图表,所以我在运行的图形上创建了一个subplot。这很好用。然而,在脚本中的某个点,一些subplots 被从图中删除,并且图中显示了一些 empty 子图。我想避免它,但我无法检索图中为空的子图。这是我的代码:
#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)
直到这里可以正常工作,但是当我显示该图时,结果如下所示:
您会注意到那里有两个空的子图......在第二个位置和第五个位置。我怎样才能避免它?或者..如何重新组织子图,使图中不再有空白?
感谢任何帮助!提前致谢。
【问题讨论】:
标签: python matplotlib networkx figure subplot