颜色Networkx和Graphviz的特定节点(Color a particular node i

2019-08-03 20:49发布

我写一个程序,从距离矩阵图的曲线图。 这是工作的罚款。 现在我想某个节点和一定的边缘是我选择的特定颜色的。 我怎么做?

该计划是在Python和使用Networkx和的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')

Answer 1:

由于您使用的Graphviz做图纸,你需要使用Graphviz的理解的属性。 见https://graphviz.gitlab.io/_pages/doc/info/attrs.html

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()

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"];
}



文章来源: Color a particular node in Networkx and Graphviz