如何在python中添加一个简单的“colorbar”到网络图?

如何在python中添加一个简单的“colorbar”到网络图?,第1张

概述我想知道如何在我的情节中添加一个颜色条.我必须遵循代码,通过读取gml文件绘制图形.我有一组数字作为颜色分配给边缘,我只想在图表旁边看到一个颜色条,这样我就可以分析颜色了.当我添加plt.colorbar(g)时,它给了我错误.如何在不经历实际构建色条的所有过程的情况下添加色条? H = nx.read_gml('./network1.gml')EAM = EigenVectorCentral 我想知道如何在我的情节中添加一个颜色条.我必须遵循代码,通过读取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 pltimport networkx as nxG=nx.star_graph(20)pos=nx.spring_layout(G)colors=range(20)cmap=plt.cm.Bluesvmin = 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有点可悲.

总结

以上是内存溢出为你收集整理的如何在python中添加一个简单的“colorbar”到网络图?全部内容,希望文章能够帮你解决如何在python中添加一个简单的“colorbar”到网络图?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1193232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存