[英]Add graphviz plot inside matplotlib
I am visualizing various networks with networkx
.我正在使用
networkx
可视化各种网络。 Take a look at simple example看一个简单的例子
graph = nx.DiGraph()
graph.add_edge("a", "b")
graph.add_edge("a", "a")
nx.draw(graph)
plt.show()
Unfortunately, networkx
plotting does not render self loops.不幸的是,
networkx
绘图不会呈现自循环。 I am aware of other feature rich packages like GraphViz
and its implementation pygraphviz
.我知道其他功能丰富的软件包,如
GraphViz
及其实现pygraphviz
。 However, such packages do not allow me to customize my plots (like subplots, annotations etc).但是,这样的包不允许我自定义我的图(如子图、注释等)。 I can do all of these with
networkx
as it can plot with matplotlib
Axes.我可以用
networkx
做所有这些,因为它可以用matplotlib
Axes 绘图。 Which is very convenient for programmatic manipulations and heavy customizations.这对于程序化操作和大量自定义非常方便。 Is there way to get network plotting of
GraphViz
to matplotlib
?有没有办法让
GraphViz
网络绘图到matplotlib
?
I can always embed PNG created by GraphViz
into matplotlib
plots using imshow
.我总是可以使用
imshow
将GraphViz
创建的 PNG 嵌入到matplotlib
图中。 However, its results are horrible with little customization control.然而,它的结果是可怕的,几乎没有定制控制。
It doesn't seem like this exists yet.这似乎还不存在。 If you want to implement it yourself, there are a few options.
如果你想自己实现它,有几个选择。 If you want to leave the layout to Graphviz and just hand shapes and positions to Matplotlib, two options remain to get the data necessary for rendering:
如果您想将布局留给 Graphviz 而只是将形状和位置交给 Matplotlib,还有两个选项可以获取渲染所需的数据:
dot
) with the -Tjson
option , which seems to emit all necessary data for drawing, but I didn't find documentation for what eg the op
s are.dot
)与-Tjson
option 一起使用,它似乎发出了所有必要的绘图数据,但我没有找到关于op
是什么的文档。 You then feed this data into the Axes
' Artist
(tutorial) .然后,您将此数据输入
Axes
的Artist
(教程) 。 Matplotlib also has a tutorial for how to draw paths . Matplotlib 也有一个关于如何绘制路径的教程。
I am not sure why you say networkx does not render self-loops.我不确定你为什么说 networkx 不呈现自循环。 Isn't this what you are looking for?
这不是你要找的吗?
import networkx as nx
from matplotlib import pyplot as plt
graph = nx.DiGraph()
graph.add_edges_from([("a", "b"), ("a", "a"), ("b", "b"), ("c", "b"), ("c", "c")])
nx.draw(graph, with_labels=True)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.