[英]graphviz: change color of some nodes
I am using NetworkX
to parse a graph and graphviz
to represent that same graph as an svg
file.我正在使用NetworkX
来解析图形,并使用graphviz
来表示与svg
文件相同的图形。 I am trying to color each node of my graph based on the name of the node such as:我正在尝试根据节点名称为图形的每个节点着色,例如:
if node.name == 'abc':
node.color = 'blue'
else:
node.color = 'pink'
I was able to color every node of my graph with the same color and I am not able to set conditions to have different node colors based on my previous algorithm.我能够用相同的颜色为图表的每个节点着色,但我无法根据我以前的算法设置条件以具有不同的节点 colors。
Based on this StackOverflow post, Graphviz dot: How to change the colour of one record in multi-record shape , it is probably not possible.基于这篇 StackOverflow 帖子, Graphviz dot: How to change the color of one record in multi-record shape ,这可能是不可能的。 But it I look at the .dot
output file, each node as a color attribute so it should be possible to change it?但是我看.dot
output 文件,每个节点作为一个颜色属性所以应该可以更改吗?
graph.dot:图.点:
digraph "" {
graph [bb="0,0,61610,509.51",
edge_default="{}",
edges="{'arrowsize': '4.0'}",
node_default="{}",
rankdir=TD
];
node [fillcolor=red,
label="\N",
shape=rectangle,
style=filled
];
"gold_adhoc_demand_review.countries" [color=red,
height=0.5,
kind=adhoc,
pos="1010,234",
table=countries,
use_case=demand_review,
width=3.1389,
zone=gold];
"gold_core.location" [color=blue,
height=0.5,
kind=core,
pos="868,306",
table=location,
width=1.6389,
zone=gold];
"gold_core.location" -> "gold_adhoc_demand_review.countries" [pos="e,975.13,252.19 902.74,287.88 921.82,278.47 945.68,266.71 966.05,256.67"];
"gold_adhoc_product_priorities.distribution_network" [color=red,
height=0.5,
kind=adhoc,
pos="150,234",
table=distribution_network,
use_case=product_priorities,
width=4.1667,
zone=gold];
"gold_core.location" -> "gold_adhoc_product_priorities.distribution_network" [pos="e,300.22,251.11 808.82,299.62 708.56,290.46 498.65,271.06 310.31,252.13"];
"gold_adhoc_global_supply_review.country" [color=red,
height=0.5,
kind=adhoc,
pos="1267,234",
table=country,
use_case=global_supply_review,
width=3.4861,
zone=gold];
...
draw.py绘图.py
def draw_graph(networkx_graph):
networkx_graph.graph['graph'] = {'rankdir': PYGRAPHIZ_RANKDIR}
networkx_graph.graph['node'] = {'shape': PYGRAPHIZ_SHAPE_TYPE, 'fillcolor': 'red', 'style':'filled'}
networkx_graph.graph['edges']={'arrowsize': PYGRAPHIZ_ARROW_SIZE}
pygraphviz_graph = to_agraph(networkx_graph)
pygraphviz_graph.layout("svg")
pygraphviz_graph.draw("result.svg")
print(pygraphviz_graph.graph_attr)
The property you want to update is fillcolor
, not color
.您要更新的属性是fillcolor
,而不是color
。 color
controls the border of a node. color
控制节点的边界。
(See the documentation at https://graphviz.org/docs/attrs/fillcolor/ ) (参见文档https://graphviz.org/docs/attrs/fillcolor/ )
Your logic would look like this to customize the fill color of individual nodes:您的逻辑看起来像这样来自定义各个节点的填充颜色:
if node.name == 'abc':
node.fillcolor = 'gold'
else:
node.fillcolor = 'pink'
Note: you can use RGB color codes rather than names to give you more options, eg: fillcolor = "#ffd700"
注意:您可以使用 RGB 颜色代码而不是名称来为您提供更多选项,例如: fillcolor = "#ffd700"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.