如何在python中添加一个简单的“colorbar”到网络图?
作者:互联网
我想知道如何在我的情节中添加一个颜色条.我必须遵循代码,通过读取gml文件绘制图形.我有一组数字作为颜色分配给边缘,我只想在图表旁边看到一个颜色条,这样我就可以分析颜色了.当我添加plt.colorbar(g)时,它给了我错误.如何在不经历实际构建色条的所有过程的情况下添加色条?
H = nx.read_gml('./network1.gml')
EAM = EigenVectorCentrality( EAMatrix );
x = [];
for eam in EAM[0]:
x.append(eam[0]);
degs = H.degree().values();
plt.clf()
g = nx.draw(H, with_labels=0, edge_color=x, node_size=70, font_size=9, width=1)
plt.axis('equal')
plt.colorbar(g);
plt.show()
这是Nerwork1.gml文件:
graph
[
node
[
id 1
]
node
[
id 2
]
node
[
id 3
]
node
[
id 4
]
node
[
id 5
]
node
[
id 6
]
node
[
id 7
]
node
[
id 8
]
node
[
id 9
]
node
[
id 10
]
node
[
id 11
]
edge
[
source 1
target 2
]
edge
[
source 1
target 2
]
edge
[
source 1
target 3
]
edge
[
source 1
target 4
]
edge
[
source 1
target 5
]
edge
[
source 2
target 3
]
edge
[
source 2
target 4
]
edge
[
source 2
target 5
]
edge
[
source 3
target 4
]
edge
[
source 3
target 5
]
edge
[
source 4
target 5
]
edge
[
source 6
target 7
]
edge
[
source 6
target 8
]
edge
[
source 6
target 9
]
edge
[
source 6
target 10
]
edge
[
source 7
target 8
]
edge
[
source 7
target 9
]
edge
[
source 7
target 10
]
edge
[
source 8
target 9
]
edge
[
source 8
target 10
]
edge
[
source 9
target 10
]
edge
[
source 5
target 6
]
edge
[
source 5
target 11
]
edge
[
source 6
target 11
]
]
解决方法:
由于我没有您的数据,我在networx主页上使用了this simple example.但是在你的代码中使用它应该是微不足道的.
import matplotlib.pyplot as plt
import networkx as nx
G=nx.star_graph(20)
pos=nx.spring_layout(G)
colors=range(20)
cmap=plt.cm.Blues
vmin = min(colors)
vmax = max(colors)
nx.draw(G, pos, node_color='#A0CBE2', edge_color=colors, width=4, edge_cmap=cmap,
with_labels=False, vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin = vmin, vmax=vmax))
sm._A = []
plt.colorbar(sm)
plt.show()
这样做的伎俩,但我同意,nx.draw只返回None有点可悲.
标签:colorbar,python,networkx 来源: https://codeday.me/bug/20190728/1560765.html