python坐标柱染色,python如何为GraphViz写一个点文件,要求一些边被着色为红色?...

I am using python code (with python nested dicts) to write out a DOT file for GraphViz to draw my directed edge-weighted graph, thanks to DAWG's suggestions...

nestedg={1: {2: 3, 3: 8, 5: -4},

2: {4: 1, 5: 7},

3: {2: 0.09},

4: {1: 2, 3: -5},

5: {4: 6}}

with open('/tmp/graph.dot','w') as out:

for line in ('digraph G {','size="16,16";','splines=true;'):

out.write('{}\n'.format(line))

for start,d in nestedg.items():

for end,weight in d.items():

out.write('{} -> {} [ label="{}" ];\n'.format(start,end,weight))

out.write('}\n')

Which produces this .DOT file, from which GraphViz can produce a nice graph image:

digraph G {

size="16,16";

splines=true;

1 -> 2 [ label="3" ];

1 -> 3 [ label="8" ];

1 -> 5 [ label="-4" ];

2 -> 4 [ label="1" ];

2 -> 5 [ label="7" ];

3 -> 2 [ label="0.09" ];

4 -> 1 [ label="2" ];

4 -> 3 [ label="-5" ];

5 -> 4 [ label="6" ];

}

QUESTION:

How can I change this python code to ask GraphViz to color an edge RED if its weight is less than a specific number (e.g., 0.5), such as the edge from node 3 to 2 (with a weight of 0.09)?

And more generally is there a good place to learn much more about how to write python code to create all kinds of DOT files for GraphViz, and to see good examples?

Thanks Tom99

解决方案

Graphviz supports color attribute.

Thus, you could modify your code to write out the line

3 -> 2 [ label="0.09" color="red" ];

to color the edge as red.

A simple way to achieve it is to modify the line

out.write('{} -> {} [ label="{}" ];\n'.format(start,end,weight))

into

if weight < 0.5:

out.write('{} -> {} [ label="{}" color="red" ];\n'.format(start,end,weight))

else:

out.write('{} -> {} [ label="{}" ];\n'.format(start,end,weight))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值