简体   繁体   English

从 pydotplus(或任何分层图形绘制引擎)检索节点位置

[英]Retrieving node locations from pydotplus (or any layered graph drawing engine)

I'm preparing a layered graph drawing using a dataframe containing node data:我正在使用包含节点数据的 dataframe 准备分层图:

    type              label
0  Class  Insurance Product
1  Class             Person
2  Class            Address
3  Class   Insurance Policy

And another containing relationship data:另一个包含关系数据:

              froml                tol             rel fromcard tocard
0  Insurance Policy  Insurance Product  ConveysProduct      One    One
1            Person   Insurance Policy       hasPolicy      One   Many
2            Person            Address       ResidesAt     None   None

I populate a pydotplus dot graph with the content, which I can then use to generate a rendering:我用内容填充一个pydotplus点图,然后我可以用它来生成渲染:

pdp_graph = pydotplus.graphviz.Dot(graph_name="pdp_graph", graph_type='digraph', prog="dot")
for i,e in b_rels_df.iterrows():
    edge = pydotplus.graphviz.Edge(src=e['froml'], dst=e['tol'], label=e['rel'])#, set_fromcard=e['fromcard'], set_tocard=e['tocard'])
    pdp_graph.add_edge(edge)
    
for i,n in ents_df.iterrows():
    node = pydotplus.graphviz.Node(name=n['label'], set_type=n['type'], set_label=n['label'])
    pdp_graph.add_node(node)

png = pdp_graph.create_png()

display(Image(png))

在此处输入图像描述

So far so good - but now I want to retrieve the node positions for use in my own interactive layout (the png is a nice example/diagram, but I want to build upon it), so am attempting to retrieve the node locations calculated via:到目前为止一切顺利 - 但现在我想检索用于我自己的交互式布局的节点位置(png 是一个很好的示例/图表,但我想在它的基础上构建),所以我试图检索通过计算的节点位置:

[n.get_pos() for n in pdp_graph.get_nodes()]

But this only returns:但这只会返回:

> [None, None, None, None]

I've tried lots of different methods, graphviz/dot are installed fine - as proven by the image of the layout - how can I extract the positions of the nodes as data from any type of dot-style layout?我尝试了很多不同的方法,graphviz/dot 安装得很好——正如布局图像所证明的那样——我如何从任何类型的点式布局中提取节点的位置作为数据?

There is a way I can do this via the pygraphviz library via.networkx, but the installation-overhead restricts me (pygraphviz needs to be recompiled to cinch with the graphviz install) from being able to use that for the target installations where I've less control over the base environments, hence my attempt to use pydotplus, which appears less demanding in terms of install requirements.有一种方法可以通过pygraphviz库 via.networkx 执行此操作,但安装开销限制了我(pygraphviz 需要重新编译以与 graphviz 安装一起使用)无法将其用于我已经安装的目标安装对基本环境的控制较少,因此我尝试使用 pydotplus,它在安装要求方面似乎要求不高。

How do I retrieve the layout data from a layered graph drawing using this setup (or one similar), such that I can use it elsewhere?如何使用此设置(或类似设置)从分层图表中检索布局数据,以便我可以在其他地方使用它? I'm looking for x,y values that I can map back to the nodes that they belong to.我正在寻找可以 map 返回到它们所属的节点的 x,y 值。

I know nothing about your python setup.我对您的 python 设置一无所知。 ( As usual with python it seems awkward and restrictive ) (和往常一样 python 看起来很尴尬和限制)

I suggest using Graphviz directly.我建议直接使用 Graphviz。 In particular the 'attributed DOT format' which is a plain text file containing the layout ( resulting from the layout engine ) produced when the engine is run with the command option -Tdot.特别是“属性 DOT 格式”,它是一个纯文本文件,包含在使用命令选项 -Tdot 运行引擎时生成的布局(由布局引擎产生)。 The text file is easily parsed to get exactly what you need.文本文件很容易解析以准确获取您需要的内容。

Here is a screenshot of the first paragraph of the relevant documentation这是相关文档第一段的屏幕截图

在此处输入图像描述

The graphviz.org website contains all the additional details you may need. graphviz.org 网站包含您可能需要的所有其他详细信息。

You are creating an output file of png format.您正在创建一个 output png 格式的文件。 All position data is lost in this process.所有 position 数据在此过程中丢失。 Instead, create output format="dot".相反,创建 output format="dot"。 Then, read that back in & modify as desired.然后,重新阅读并根据需要进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM