本文整理汇总了Python中pygraphviz.AGraph.subgraphs方法的典型用法代码示例。如果您正苦于以下问题:Python AGraph.subgraphs方法的具体用法?Python AGraph.subgraphs怎么用?Python AGraph.subgraphs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygraphviz.AGraph
的用法示例。
在下文中一共展示了AGraph.subgraphs方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dot_layout
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import subgraphs [as 别名]
def dot_layout(cy_elements, edge_labels=False, subgraph_boxes=False, node_gt=None):
"""
Get a CyElements object and augment it (in-place) with positions,
widths, heights, and spline data from a dot based layout.
edge_labels is true if labels should appear on edges
subgraph_boxes is true if boxes should be drawn around subgraphs
Returns the object.
"""
elements = cy_elements.elements
# g = AGraph(directed=True, strict=False)
g = AGraph(directed=True, strict=False, forcelabels=True)
# make transitive relations appear top to bottom
elements = list(elements)
nodes_by_id = dict((e["data"]["id"], e) for e in elements if e["group"] == "nodes")
order = [
(nodes_by_id[e["data"]["source"]], nodes_by_id[e["data"]["target"]])
for e in elements
if e["group"] == "edges" and "transitive" in e["data"] and e["data"]["transitive"]
]
elements = topological_sort(elements, order, lambda e: e["data"]["id"])
# get the node id's and stable sort them by cluster
# the idea here is to convert the graph into a dag by sorting
# the nodes, then reversing the back edges. In particular, we try to make
# all the edges between two clusters go in the same direction so clustering
# doesn't result in horizontal edges, which dot renders badly.
sorted_nodes = [e["data"]["id"] for e in elements if e["group"] == "nodes"]
sorted_nodes = sorted(enumerate(sorted_nodes), key=lambda x: (nodes_by_id[x[1]]["data"]["cluster"], x[0]))
sorted_nodes = [y for idx, y in sorted_nodes]
node_key = dict((id, idx) for idx, id in enumerate(sorted_nodes))
if node_gt is None:
node_gt = lambda X, y: False
else:
node_gt = lambda x, y: node_key[x] > node_key[y]
# add nodes to the graph
for e in elements:
if e["group"] == "nodes" and e["classes"] != "non_existing":
g.add_node(e["data"]["id"], label=e["data"]["label"].replace("\n", "\\n"))
# TODO: remove this, it's specific to leader_demo
weight = {"reach": 10, "le": 10, "id": 1}
constraint = {"pending": False}
# add edges to the graph
for e in elements:
if e["group"] == "edges":
# kwargs = {'weight': weight.get(e["data"]["obj"], 0)},
kwargs = {"label": e["data"]["label"]} if edge_labels else {}
if node_gt(e["data"]["source"], e["data"]["target"]):
g.add_edge(
e["data"]["target"],
e["data"]["source"],
e["data"]["id"],
dir="back",
**kwargs
# constraint=constraint.get(e["data"]["obj"], True),
)
else:
g.add_edge(
e["data"]["source"],
e["data"]["target"],
e["data"]["id"],
**kwargs
# constraint=constraint.get(e["data"]["obj"], True),
)
# add clusters
clusters = defaultdict(list)
for e in elements:
if e["group"] == "nodes" and e["data"]["cluster"] is not None and e["classes"] != "non_existing":
clusters[e["data"]["cluster"]].append(e["data"]["id"])
for i, k in enumerate(sorted(clusters.keys())):
g.add_subgraph(name="cluster_{}".format(i), nbunch=clusters[k], rank="min")
# now get positions, heights, widths, and bsplines
g.layout(prog="dot")
# get the y origin. we want the top left of the graph to be a
# fixed coordinate (hopefully (0,0)) so the graph doesn't jump when
# its height changes. Unfortunately, pygraphviz has a bug a gives
# the wrong bbox, so we compute the max y coord.
# bbox = pygraphviz.graphviz.agget(g.handle,'bb')
global y_origin
y_origin = 0.0
for n in g.nodes():
top = float(n.attr["pos"].split(",")[1]) + float(n.attr["height"]) / 2
if top > y_origin:
y_origin = top
if subgraph_boxes:
for sg in g.subgraphs():
#.........这里部分代码省略.........