File: plot_grid.py

package info (click to toggle)
networkx 3.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,700 kB
  • sloc: python: 105,310; xml: 544; makefile: 131; javascript: 120; sh: 34
file content (24 lines) | stat: -rw-r--r-- 715 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
2D Grid
=======

Illustrate `~networkx.drawing.nx_agraph.write_dot` in conjunction with the
Graphviz command line interface to create visualizations.
"""

import matplotlib.pyplot as plt
import networkx as nx

# %%
# Write a dot file from a networkx graph for further processing with graphviz.

G = nx.grid_2d_graph(5, 5)  # 5x5 grid
# This example needs Graphviz and PyGraphviz
nx.nx_agraph.write_dot(G, "grid.dot")
# Having created the dot file, graphviz can be invoked via the command line
# to generate an image on disk, e.g.
print("Now run: neato -Tps grid.dot >grid.ps")

# Alternatively, the and image can be created directly via AGraph.draw
A = nx.nx_agraph.to_agraph(G)
A.draw("5x5.png", prog="neato")