如何使用Python的pydot库合并两个有向图?
Got it, let's work through how to merge your two pydot digraphs into the combined graph you need. I'll cover two common approaches depending on what your "specified style" entails—either a single flat graph with all nodes and edges, or clustered subgraphs that keep the original two graphs visually separated.
Approach 1: Merge into a Single Flat Digraph
If you just want all nodes and edges from both graphs in one unified digraph (no visual separation between the original two), you can iterate through each graph's nodes and edges and add them to a new combined graph:
import pydot # First, recreate your original graphs graph1 = pydot.Dot(graph_type='digraph') A = pydot.Node("A", style="filled", fillcolor="green") B = pydot.Node("B", style="filled", fillcolor="blue") graph1.add_node(A) graph1.add_node(B) graph1.add_edge(pydot.Edge(A,B)) graph2 = pydot.Dot(graph_type='digraph') C = pydot.Node("C", style="filled", fillcolor="green") D = pydot.Node("D", style="filled", fillcolor="blue") graph2.add_node(C) graph2.add_node(D) graph2.add_edge(pydot.Edge(C,D)) # Create a new empty digraph for the combined result combined_graph = pydot.Dot(graph_type='digraph') # Add all nodes and edges from graph1 for node in graph1.get_nodes(): combined_graph.add_node(node) for edge in graph1.get_edges(): combined_graph.add_edge(edge) # Add all nodes and edges from graph2 for node in graph2.get_nodes(): combined_graph.add_node(node) for edge in graph2.get_edges(): combined_graph.add_edge(edge) # Save the merged graph combined_graph.write_png('combined_flat.png')
Approach 2: Merge as Clustered Subgraphs
If you want the original two graphs to stay visually grouped (like side-by-side boxes), use pydot's Subgraph with a cluster_ prefix—this tells Graphviz to render them as separate clusters:
import pydot # Recreate original graphs graph1 = pydot.Dot(graph_type='digraph') A = pydot.Node("A", style="filled", fillcolor="green") B = pydot.Node("B", style="filled", fillcolor="blue") graph1.add_node(A) graph1.add_node(B) graph1.add_edge(pydot.Edge(A,B)) graph2 = pydot.Dot(graph_type='digraph') C = pydot.Node("C", style="filled", fillcolor="green") D = pydot.Node("D", style="filled", fillcolor="blue") graph2.add_node(C) graph2.add_node(D) graph2.add_edge(pydot.Edge(C,D)) # Create combined digraph (use rankdir="LR" for horizontal cluster layout) combined_clustered = pydot.Dot(graph_type='digraph', rankdir="LR") # Create first cluster subgraph (name must start with cluster_) subgraph1 = pydot.Subgraph("cluster_graph1", label="Original Graph 1", style="filled", color="lightgrey") for node in graph1.get_nodes(): subgraph1.add_node(node) for edge in graph1.get_edges(): subgraph1.add_edge(edge) combined_clustered.add_subgraph(subgraph1) # Create second cluster subgraph subgraph2 = pydot.Subgraph("cluster_graph2", label="Original Graph 2", style="filled", color="lightgrey") for node in graph2.get_nodes(): subgraph2.add_node(node) for edge in graph2.get_edges(): subgraph2.add_edge(edge) combined_clustered.add_subgraph(subgraph2) # Optional: Add an edge between the two clusters (e.g., from B to C) combined_clustered.add_edge(pydot.Edge(B, C)) # Save the clustered merged graph combined_clustered.write_png('combined_clustered.png')
Key notes:
- For clustered subgraphs, the subgraph name must start with
cluster_—this is a Graphviz convention that triggers the boxed cluster rendering. - Adjust
rankdir(e.g., "TB" for top-bottom, "LR" for left-right) to control the layout of your clusters. - If your "specified style" has other requirements (like shared nodes, custom spacing, etc.), tweak the code further based on those needs!
内容的提问来源于stack exchange,提问作者user1999109

