使用draw_networkx(),如何显示多个绘图窗口?

9
以下代码一次只会创建一个窗口,第二个窗口只有在用户关闭第一个窗口后才会显示。
如何同时显示它们并具有不同的标题?
nx.draw_networkx(..a..)
nx.draw_networkx(..b..)
2个回答

21

使用Matplotlib制作其他图形的方法是相同的。使用figure()命令切换到新的图形。

import networkx as nx
import matplotlib.pyplot as plt

G=nx.cycle_graph(4)
H=nx.path_graph(4)

plt.figure(1)
nx.draw(G)
plt.figure(2)
nx.draw(H)

plt.show()

顺便问一下,在显示图形之前如何设置画布大小? - Matt
2
figure()函数接受一个尺寸参数:figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')。 - Aric
2
如果您想在两个子图中并排绘制两个网络,该怎么办? - FaCoffee

3
您可以使用Matplotlib和网格来显示多个图形:
#!/usr/bin/env python
"""
Draw a graph with matplotlib.
You must have matplotlib for this to work.
"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
#    Copyright (C) 2004-2008
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    All rights reserved.
#    BSD license.

try:
    import matplotlib.pyplot as plt
except:
    raise

import networkx as nx

G=nx.grid_2d_graph(4,4)  #4x4 grid

pos=nx.spring_layout(G,iterations=100)

plt.subplot(221)
nx.draw(G,pos,font_size=8)

plt.subplot(222)
nx.draw(G,pos,node_color='k',node_size=0,with_labels=False)

plt.subplot(223)
nx.draw(G,pos,node_color='g',node_size=250,with_labels=False,width=6)

plt.subplot(224)
H=G.to_directed()
nx.draw(H,pos,node_color='b',node_size=20,with_labels=False)

plt.savefig("four_grids.png")
plt.show()

上面的代码将生成以下图表:

输入图像描述

参考文献: https://networkx.org/documentation/networkx-1.9/examples/drawing/four_grids.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接