在Networkx和Graphviz中为特定节点着色 [英] Color a particular node in Networkx and Graphviz
本文介绍了在Networkx和Graphviz中为特定节点着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序来绘制距离矩阵的图形。它工作正常。
现在我想要一个特定的节点和一个特定的边缘是我选择的特定颜色。我该怎么做?
这个程序使用Python,使用Networkx和Graphviz
import networkx as nx
$ p $因为您正在使用Graphviz来执行绘图,所以您需要使用Graphviz可以理解的属性。查看 http://www.graphviz.org/content/attrs
import numpy as np
import pickle
from random import randint
p_file = open('pickles / distance')
Dist = pickle.load(p_file)
p_file.close()
p_file = open('pickles / names')
Names = pickle.load(p_file)
p_file.close()
dt = [('len',float)]
A = np.array(Dist)* 5
A = A.view(dt)
G = nx.from_numpy_matrix(A)
G = nx.relabel_nodes(G,dict(zip(range(len(G.nodes())),Names)))
G = nx.to_agraph(G)
G.node_attr.update(ndcolor =red,node =DC,style =filled)
G.edge_attr。 update(color =none)
G.draw('P1.png',format ='png',prog ='neato')
导入networkx为nx
G = nx.Graph()
G.add_node(1,color ='red ',style ='filled',fillcolor ='blue',shape ='square')
G.add_node(2,color ='blue',style ='filled')
G.add_edge 1,2,color ='green')
G.node [2] ['shape'] ='circle'
G.node [2] ['fillcolor'] ='red'
A = nx.to_agraph(G)
A.layout()
A.draw('color.png')
print A.to_string()
给出
严格图{
图[bb =0,0,107.21,46.639];
node [label =\N];
1 [color = red,
fillcolor = blue,
height = 0.5,
pos =18,28.639,
shape = square,
style = filled,
width = 0.5];
2 [color = blue,
fillcolor = red,
height = 0.5,
pos =89.21,18,
shape = circle,
style = filled,
width = 0.5];
1 - 2 [color = green,
pos =36.338,25.899 47.053,24.298 60.519,22.286 71.18,20.694];
}
I am writing a program to plot a graph from a distance matrix. It is working fine. Now I want a certain node and a certain edge to be of a particular color of my choice. How do I do that?
The program is in Python and uses Networkx and Graphviz
import networkx as nx import numpy as np import pickle from random import randint p_file = open('pickles/distance') Dist = pickle.load(p_file) p_file.close() p_file = open('pickles/names') Names = pickle.load(p_file) p_file.close() dt = [('len', float)] A = np.array(Dist)*5 A = A.view(dt) G = nx.from_numpy_matrix(A) G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),Names))) G = nx.to_agraph(G) G.node_attr.update(ndcolor="red", node="DC", style="filled") G.edge_attr.update(color="none") G.draw('P1.png', format='png', prog='neato')
解决方案Since you are using Graphviz to do the drawing you need to use the attributes that Graphviz understands. See http://www.graphviz.org/content/attrs
import networkx as nx G = nx.Graph() G.add_node(1,color='red',style='filled',fillcolor='blue',shape='square') G.add_node(2,color='blue',style='filled') G.add_edge(1,2,color='green') G.node[2]['shape']='circle' G.node[2]['fillcolor']='red' A = nx.to_agraph(G) A.layout() A.draw('color.png') print A.to_string()
Gives
strict graph { graph [bb="0,0,107.21,46.639"]; node [label="\N"]; 1 [color=red, fillcolor=blue, height=0.5, pos="18,28.639", shape=square, style=filled, width=0.5]; 2 [color=blue, fillcolor=red, height=0.5, pos="89.21,18", shape=circle, style=filled, width=0.5]; 1 -- 2 [color=green, pos="36.338,25.899 47.053,24.298 60.519,22.286 71.18,20.694"]; }
这篇关于在Networkx和Graphviz中为特定节点着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文