This article summarizes how to visualize graphs created with NetworkX
, a package for creating graphs (in graph theory with nodes and edges) in Python, with Matplotlib.
Basically, it is a Japanese translation of the NetworkX reference.
There are two ways to draw a graph created with NetworkX.
networkx.drawing
package
Please note that the networkx.drawing
package is not yet compatible with Python versions 3.0 and above.If you want to draw using Matplotlib, please import Matplotlib.
from matplotlib import pyplot as plt
To see if the imported networkx.drawing package works, try running the following code
nx.draw(G)
nx.draw_random(G)
nx.draw_circular(G)
nx.draw_spectral(G)
To show it on the display, execute the following command.
plt.show()
If you want to save the drawn graph to a file, do as follows.
nx.draw(G)
plt.savefig("path_to_fig.png ")
References
Recommended Posts