Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从包含有向图的.dot文件创建图形_Python_Python 3.x_Graphviz - Fatal编程技术网

Python 从包含有向图的.dot文件创建图形

Python 从包含有向图的.dot文件创建图形,python,python-3.x,graphviz,Python,Python 3.x,Graphviz,我有一个.dot文件,希望以可视图形的形式显示有向图的内容 当我运行它时,总是会出错。我看过很多帖子,但没有找到有效的解决方案 import graphviz as pgv import networkx as nx import matplotlib as plt def load_json(filename): import json with open(filename, 'r') as file: content = "\n".join(file.rea

我有一个.dot文件,希望以可视图形的形式显示有向图的内容 当我运行它时,总是会出错。我看过很多帖子,但没有找到有效的解决方案

import graphviz as pgv
import networkx as nx
import matplotlib as plt

def load_json(filename):
    import json
    with open(filename, 'r') as file:
        content = "\n".join(file.readlines())
        return json.loads(content)


distance_graph = load_json('../Main/distanceTables.json')

with open('../Main/distanceTable.dot', 'wt') as file:
    print('Writing: ../Main/distanceTable.dot')
    file.write('digraph {\n')
    for city in distance_graph.keys():
        for adjacent_city in distance_graph[city].keys():
            distance = distance_graph[city][adjacent_city]
            file.write('\t"{}" -> "{}"[label="{}",weight="{}"];\n'.format(city, adjacent_city, distance, distance))
    file.write("}\n")

Gtemp = pgv.Digraph('../Main/distanceTable.dot')
G = nx.Graph(Gtemp)
nx.draw(G)
plt.show()
错误:to_networkx_图形中的第165行 “输入不是用于转换的已知数据类型。”)
networkx.exception.NetworkXError:输入不是用于转换的已知数据类型。

使用networkx时,是否有原因手动创建.dot文件,然后将其读回?从json数据创建一个networkx图形会更简单,然后根据需要使用它。如果您仍然需要一个点文件,请在networkx上使用nx.nx\u agraph.write\u点graph@Jon克莱门茨在另一篇文章中,我看到他们是这样写的,所以我试了一下。我确实想使用json数据,但我不知道该怎么做。