如何添加简单的“颜色栏"到python中的网络图图? [英] How to add a simple "colorbar" to a network-graph plot in python?

查看:172
本文介绍了如何添加简单的“颜色栏"到python中的网络图图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何简单地将彩条添加到绘图中.我必须遵循以下代码,该代码通过读取gml文件来绘制图形.我有一组数字分配给边缘作为其颜色,我只想在图旁边看到一个色条,以便我分析颜色.当我添加plt.colorbar(g)时,它给我错误.我如何在不完成实际构建颜色条的所有过程的情况下添加颜色条?

I was wondering how can I simply add a colorbar to my plot. I have to following code, which plots a graph by reading a gml file. I have a set of numbers to assign to the edges as their colors and I just want to see a colorbar right next to the plot so I can analyze the colors. When I add the plt.colorbar(g) it gives me error. How can I add a colorbar without going through all the process of actually building the colorbar?

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文件:

And here is the Nerwork1.gml file:

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
 ]
]

推荐答案

由于我没有可用的数据,因此我使用了

Since I did not have your data available, I used this simple example from the networx homepage. But it should be trivial for you to use it in your code.

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有点可悲.

This does the trick, but I agree, it is a bit sad that nx.draw just returns None.

这篇关于如何添加简单的“颜色栏"到python中的网络图图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆