无法使用graphviz创建图

我有以下示例json数据。我正在遍历它。我也获得了理想的输出,但是无法从中生成图形。我想要一个图表来可视化每个节点边缘的数据。

from __future__ import print_function
import json
from graphviz import Graph
import sys
# Tree in JSON format
#s = '{"Harry": {"children": ["Bill",{"Jane": {"children": [{"Diane": {"children": ["Mary"]}},"Mark"]}}]}}'
#s = '{"Harry": {"children": ["3Bi_ll<=0.5 samplesize=10","Mark"]}}]}}'
#{'pd_a6 <= 2.5':
# Convert JSON tree to a Python dict
TREEDATA = {'pd_a6 <= 2.5':
[{'2pd_count <= 1.9767665617780872':
[{'limit_bal <= 36181.54239732496':
[{'rev_m5 = 1.0': [1,0]},{'pd_m6 = 1.0': [1,0]}]},{'limit_bal <= 50002.424881786734':
[1,{'pd_m6 = 1.0':
[1,{'limit_bal <= 63514.92986216776': [1,0]}]}]}]},{'rev_a6 <= 1.5':
[{'rev_m1 = 1':
[{'1pd_count <= 0.8656180257162627':
[1,{'limit_bal <= 29954.87122417677':
[1,1]},1]}]}

def preOrderTraversal(pyTree):
    for key,value in pyTree.items():
        print(key)
        if isinstance(value,dict):
            preOrderTraversal(value)
#s = '{"pd_a6 <= 2.5":[{2pd_count <= 1.9767665617780872":[{"limit_bal <= 36181.54239732496":[{"rev_m5 = 1.0": [1,{"pd_m6 = 1.0": [1,{"limit_bal <= 50002.424881786734":[1,{"pd_m6 = 1.0":[1,{"limit_bal <= 63514.92986216776": [1,{"rev_a6 <= 1.5":[{"rev_m1 = 1":[{"1pd_count <= 0.8656180257162627":[1,{"limit_bal <= 29954.87122417677":[1,1]}]}'
#data = json.loads(s)
print(json.dumps(TREEDATA,indent=5))
# Convert back to JSON & print to stderr so we can verify that the tree is correct.
#print(json.dumps(data,indent=4),file=sys.stderr)

# Extract tree edges from the dict
edges = []

def get_edges(treedict,parent=None):
    name = next(iter(treedict.keys()))
    if parent is not None:
        edges.append((parent,name))
    for item in treedict[name]:
        if isinstance(item,dict):
            get_edges(item,parent=name)
        else:
            edges.append((name,item))

get_edges(TREEDATA)
#xyz =get_edges(TREEDATA)
# Dump edge list in Graphviz DOT format
print('strict digraph tree {')
for row in edges:
    print('    {0} -> {1};'.format(*row))
print('}')

TREEDATA = Graph(format='png')
TREEDATA.attr('node',shape='box')
TREEDATA.render("graph-")
christianshengbing 回答:无法使用graphviz创建图

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3159764.html

大家都在问