简体   繁体   English

Graphviz/Python:单个节点生成后重新着色

[英]Graphviz/Python: Recoloring a single node after it has been generated

I am getting acquainted with graphviz within Python 2.7.我开始熟悉 Python 2.7 中的 graphviz。 Is it possible to change the properties of a single node after it has been drawn?是否可以在绘制后更改单个节点的属性?

eg例如

from graphviz import Digraph
q = Digraph()
q.node('a')
q.node('b')
q.edge('a','b')

q

简单图形的输出

Is it possible to change the color of node 'b' after the fact?是否可以在事后更改节点“b”的颜色? I am aware that I can set it at the time of generation by我知道我可以在生成时设置它

q.node('b', color = 'blue')

But, I'd like to be able to change it after generating it.但是,我希望能够在生成后更改它。

This link Color a particular node in Networkx and Graphviz此链接为 Networkx 和 Graphviz 中的特定节点着色

suggests using the graph's .node property to update a dict建议使用图形的 .node 属性来更新字典

G.node[2]['fillcolor']='red'

By analogy, I tried以此类推,我试过了

q.node['b']['color'] = 'blue'

which gives an error:这给出了一个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-43b05071d09a> in <module>()
----> 1 q.node['b']['color'] = 'blue'

TypeError: 'instancemethod' object has no attribute '__getitem__'

I think this may be because I am not using networkx as in the previous case.我认为这可能是因为我没有像前一种情况那样使用 networkx。

I've also read the graphviz docs at http://graphviz.org/content/attrs but none of my experiments have worked.我还阅读了http://graphviz.org/content/attrs 上的 graphviz 文档,但我的实验都没有奏效。 I'm sure it is something straightforward but I am missing it...我确定它很简单,但我很想念它......

--- Old Guy In The Club --- 俱乐部里的老家伙

I've read through the API docs and don't think it's possible to edit a node after construction.我已经通读了 API 文档,并且认为在构建后无法编辑节点。

If you don't want to use networkx, one workaround would be to store the graph in your own data structure and use that to create the graphviz graph at the point when you're ready to (re-)render it.如果您不想使用 networkx,一种解决方法是将图形存储在您自己的数据结构中,并在您准备(重新)渲染它时使用它来创建 graphviz 图形。 For example, you could keep a list of the names of all the nodes that should be blue, and then refer to that at the point of constructing your graphviz graph.例如,您可以保留所有应该为蓝色的节点的名称列表,然后在构建 graphviz 图时参考该列表。 Separating the model from its rendering in this way might also lead to easier maintenance.以这种方式将模型与其渲染分离也可能使维护更容易。

One way to do it is by editing the graph object directly.一种方法是直接编辑图形对象。

>>> from graphviztest import *
>>> import json
>>> dot = Digraph(comment='My graph', format='png')
>>> dot.node('A', 'hurr')
>>> dot.node('B', 'durr')
>>> dot.edge('A','B')
>>> print dot
// My graph
digraph {
    A [label=hurr]
    B [label=durr]
    A -> B
}
>>> print json.dumps(dot.__dict__, indent=2)
{
  "comment": "My graph", 
  "_encoding": "utf-8", 
  "name": null, 
  "edge_attr": {}, 
  "_format": "png", 
  "body": [
    "\tA [label=hurr]", 
    "\tB [label=durr]", 
    "\tA -> B"
  ], 
  "filename": "Digraph.gv", 
  "graph_attr": {}, 
  "strict": false, 
  "node_attr": {}
}
>>> dot.body[0] = '\tA [label=derp]'
>>> dot.body[1] = '\tB [label=blah]'
>>> print dot
// My graph
digraph {
    A [label=derp]
    B [label=blah]
    A -> B
}
>>> 


You could also save the resulting graph in SVG and edit the SVG directly, changing the color of a given node using CSS.您还可以将生成的图形保存在 SVG 中并直接编辑 SVG,使用 CSS 更改给定节点的颜色。 In SVG, generated nodes have ids, making it easy to change the color (stroke attribute).在 SVG 中,生成的节点有 id,可以很容易地改变颜色(笔触属性)。

If you set your output format to dot , the layout is calculated, but not actually drawn.如果将输出格式设置为dot ,则会计算布局,但不会实际绘制。 You can then modify that (augmented) Graphviz file with Python, sed, awk, or gvpr ( https://www.graphviz.org/pdf/gvpr.1.pdf ) to modify colors and/or other attributes.然后,您可以使用 Python、sed、awk 或 gvpr ( https://www.graphviz.org/pdf/gvpr.1.pdf ) 修改该(增强的)Graphviz 文件以修改颜色和/或其他属性。
Then use this command to draw the graph: neato -n2 -Tpng myfile.dot >myfile.png然后使用此命令绘制图形: neato -n2 -Tpng myfile.dot >myfile.png
See here for more info on delayed drawing: https://www.graphviz.org/faq/#FaqDotWithCoordsor有关延迟绘图的更多信息,请参见此处: https : //www.graphviz.org/faq/#FaqDotWithCoordsor

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

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