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

发布于 2024-11-17 10:30:53 字数 141 浏览 2 评论 0原文

以下代码一次仅创建一个窗口,第二个窗口仅在用户关闭第一个窗口时才会显示。

如何用不同的标题同时显示它们?

nx.draw_networkx(..a..)
nx.draw_networkx(..b..)

The following code will create only one window at a time, the second window will only show when the first one is closed by the user.

How to show them at the same time with different titles?

nx.draw_networkx(..a..)
nx.draw_networkx(..b..)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2024-11-24 10:30:53

它的工作原理与使用 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()

It works the same as making other plots with Matplotlib.
Use the figure() command to switch to a new 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()
橘亓 2024-11-24 10:30:53

您可以使用 matplotlib 和网格来显示多个图形:

#!/usr/bin/env python
"""
Draw a graph with matplotlib.
You must have matplotlib for this to work.
"""
__author__ = """Aric Hagberg ([email protected])"""
#    Copyright (C) 2004-2008
#    Aric Hagberg <[email protected]>
#    Dan Schult <[email protected]>
#    Pieter Swart <[email protected]>
#    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

You can use matplotlib and a grid to show multiple graphs:

#!/usr/bin/env python
"""
Draw a graph with matplotlib.
You must have matplotlib for this to work.
"""
__author__ = """Aric Hagberg ([email protected])"""
#    Copyright (C) 2004-2008
#    Aric Hagberg <[email protected]>
#    Dan Schult <[email protected]>
#    Pieter Swart <[email protected]>
#    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()

Code above will generates this figure:

enter image description here

Reference: https://networkx.org/documentation/networkx-1.9/examples/drawing/four_grids.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文