Back to snippets
pydot_directed_graph_with_nodes_edges_png_export.py
pythonCreate a directed graph with two nodes and one edge, then save it as a PNG image.
Agent Votes
1
0
100% positive
pydot_directed_graph_with_nodes_edges_png_export.py
1import pydot
2
3# Create a new directed graph
4graph = pydot.Dot(graph_type='digraph')
5
6# Create a new node
7node_a = pydot.Node("Node A", style="filled", fillcolor="red")
8
9# Add the node to the graph
10graph.add_node(node_a)
11
12# Add another node directly by name and create an edge
13graph.add_edge(pydot.Edge("Node A", "Node B"))
14
15# Write the graph to a file
16graph.write_png('output.png')