networkx

Python networkx graph: Do not draw old graph together with new graph

梦想的初衷 提交于 2021-01-22 12:12:30
问题 The below is my code: import networkx as nx for i in range(2): G = nx.DiGraph() if i==0: G.add_edge("A", "B") elif i==1: G.add_edge("A", "C") import matplotlib.pyplot as plt nx.draw(G) plt.savefig(str(i)+".png") G.clear() It should draw line AB in file 0.png and draw line AC in file 1.png. But, after I ran it. In 0.png, there is one line AB, but in 1.png, there are two lines: AB and AC. It seems that the memory for 0.png is not cleaned, although I have had "G.clear()". Does anybody know how

Python networkx graph: Do not draw old graph together with new graph

故事扮演 提交于 2021-01-22 12:10:34
问题 The below is my code: import networkx as nx for i in range(2): G = nx.DiGraph() if i==0: G.add_edge("A", "B") elif i==1: G.add_edge("A", "C") import matplotlib.pyplot as plt nx.draw(G) plt.savefig(str(i)+".png") G.clear() It should draw line AB in file 0.png and draw line AC in file 1.png. But, after I ran it. In 0.png, there is one line AB, but in 1.png, there are two lines: AB and AC. It seems that the memory for 0.png is not cleaned, although I have had "G.clear()". Does anybody know how

Python networkx graph: Do not draw old graph together with new graph

浪子不回头ぞ 提交于 2021-01-22 12:10:21
问题 The below is my code: import networkx as nx for i in range(2): G = nx.DiGraph() if i==0: G.add_edge("A", "B") elif i==1: G.add_edge("A", "C") import matplotlib.pyplot as plt nx.draw(G) plt.savefig(str(i)+".png") G.clear() It should draw line AB in file 0.png and draw line AC in file 1.png. But, after I ran it. In 0.png, there is one line AB, but in 1.png, there are two lines: AB and AC. It seems that the memory for 0.png is not cleaned, although I have had "G.clear()". Does anybody know how

How to draw a tree more beautifully in networkx

感情迁移 提交于 2021-01-21 05:16:09
问题 If I make a tree using networkx and draw it, the nodes overlap. Is there a way to draw it so there is no overlap? import matplotlib.pyplot as plt import networkx as nx T = nx.generators.balanced_tree(2, 5) nx.draw(T) plt.show() 回答1: I am no expert in this, but here is code that uses the pydot library and its graph_viz dependency. These libraries come with Anaconda Python but are not installed by default, so first do this from the command prompt: conda install pydot Then here is code adapted

How to draw a tree more beautifully in networkx

扶醉桌前 提交于 2021-01-21 05:15:40
问题 If I make a tree using networkx and draw it, the nodes overlap. Is there a way to draw it so there is no overlap? import matplotlib.pyplot as plt import networkx as nx T = nx.generators.balanced_tree(2, 5) nx.draw(T) plt.show() 回答1: I am no expert in this, but here is code that uses the pydot library and its graph_viz dependency. These libraries come with Anaconda Python but are not installed by default, so first do this from the command prompt: conda install pydot Then here is code adapted

Draw nodes in a graph clustered based on color

好久不见. 提交于 2021-01-15 10:08:46
问题 In the following dictionary mapping nodes to color, I wanted to draw the resultant graph whilst clustering the nodes within the graph based on their color. That is if node 4187 and 8285 have crimson color, I want them to appear next to each other on the graph. My dictionary is as follows: nodesWithGroup= {6: 'slateblue', 10: 'skyblue', 4109: 'lawngreen', 2062: 'mediumaquamarine', 28: 'olive', 10269: 'crimson', 6175: 'aqua', 10271: 'crimson', 36: 'mediumaquamarine', 2085: 'darkturquoise', 2084

Draw nodes in a graph clustered based on color

给你一囗甜甜゛ 提交于 2021-01-15 10:08:04
问题 In the following dictionary mapping nodes to color, I wanted to draw the resultant graph whilst clustering the nodes within the graph based on their color. That is if node 4187 and 8285 have crimson color, I want them to appear next to each other on the graph. My dictionary is as follows: nodesWithGroup= {6: 'slateblue', 10: 'skyblue', 4109: 'lawngreen', 2062: 'mediumaquamarine', 28: 'olive', 10269: 'crimson', 6175: 'aqua', 10271: 'crimson', 36: 'mediumaquamarine', 2085: 'darkturquoise', 2084

NetworkX系列教程(10)-算法之四:拓扑排序与最大流问题

我的梦境 提交于 2021-01-14 06:34:42
小书匠 Graph 图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把 图论 中常用算法弄个明白在写这部分. 图论常用算法看我的博客: 下面我将使用NetworkX实现上面的算法,建议不清楚的部分打开两篇博客对照理解. 我将图论的经典问题及常用算法的总结写在下面两篇博客中: 图论---问题篇 图论---算法篇 目录: * 11.4拓扑排序算法(TSA) * 11.5最大流问题 注意: 如果代码出现找不库,请返回第一个教程,把库文件导入. 11.4拓扑排序算法(TSA) DG = nx.DiGraph([( 'a' , 'b' ), ( 'a' , 'c' ),( 'b' , 'e' ), ( 'b' , 'd' ),( 'c' , 'e' ), ( 'c' , 'd' ),( 'd' , 'f' ), ( 'f' , 'g' ), ( 'e' , 'g' )]) #显示graph nx.draw_spring(DG,with_labels= True ) plt.title( '有向无环图' ,fontproperties=myfont) plt.axis( 'on' ) plt.xticks([]) plt.yticks([]) plt.show()

NetworkX 使用(二)

纵然是瞬间 提交于 2021-01-13 18:45:30
官方教程 博客:NetworkX %pylab inline import networkx as nx Populating the interactive namespace from numpy and matplotlib # 创建一个空图 G = nx.Graph() 节点 G.add_node(1) # 添加一个节点 G.node NodeView((1,)) G.add_nodes_from([2, 3]) # 添加多个节点 G.node NodeView((1, 2, 3)) H 是一个有 $10$ 个节点的链状图,即有 $n$ 个节点 $n-1$ 条边的连通图: H = nx.path_graph(10) nx.draw(H) plt.show() # 从 nbunch 中添加节点 G.add_nodes_from(H) # 或者 G.add_nodes_from(H.nodes()) G1 = nx.Graph() G1.add_node(H) # 将 H 当作节点 nx.draw(G1) # 仅仅只有一个节点 现在图 G1 就包含了一个节点 H , 即该节点是一个图。可以看到这种灵活性是非常强大的,它允许图的图,文件的图,函数的图等等。因此我们应该好好思考如何构建我们的应用程序才能使我们的节点是有用的实体

Python包

牧云@^-^@ 提交于 2021-01-13 18:37:42
networkx是Python的一个包,用于构建和操作复杂的图结构,提供分析图的算法。图是由顶点、边和可选的属性构成的数据结构,顶点表示数据,边是由两个顶点唯一确定的,表示两个顶点之间的关系。顶点和边也可以拥有更多的属性,以存储更多的信息。 对于networkx创建的无向图,允许一条边的两个顶点是相同的,即允许出现自循环,但是不允许两个顶点之间存在多条边,即出现平行边。边和顶点都可以有自定义的属性,属性称作边和顶点的数据,每一个属性都是一个Key:Value对。 一,创建图 在创建图之前,需要导入networkx模块,通常设置别名为nx;如果创建的图中,顶点之间的边没有方向,那么该图称作无向图。在创建图时,可以通过help(g)来获得图的帮助文档。 import networkx as nx g =nx.Graph() # 创建空的无向图 g=nx.DiGraph() # 创建空的有向图 二,图的顶点 图中的每一个顶点Node都有一个关键的ID属性,用于唯一标识一个节点,ID属性可以整数或字符类型;顶点除了ID属性之外,还可以自定义其他的属性。 1,向图中增加顶点 在向图中增加顶点时,可以一次增加一个顶点,也可以一次性增加多个顶点,顶点的ID属性是必需的。在添加顶点之后,可以通过g.nodes()函数获得图的所有顶点的视图,返回的实际上NodeView对象;如果为g.nodes