DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

NetworkX built-in graph generators

Previously, I demonstrated how to load graph data from a json file and might be useful as a reference if you have any other graph data in JSON format. But if you want to play around with any kind of graphs without bothering yourself to put them first on a JSON file, you can use the networkx generators which contain graph that cover any case you need.

The documentation for the generator can be seen here. For a starter, you may want to use the graph Small category. These small graphs are useful if you want to understand a graph from a simple example as possible. So, let's try one of those:

import networkx as nx
import matplotlib.pyplot as plt

krackhardt_kite_graph = nx.generators.krackhardt_kite_graph()

nx.draw(krackhardt_kite_graph)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Even the Les Miserables graph from my previous post is there as well in Social Network category (I should have known this...)

import networkx as nx
import matplotlib.pyplot as plt

les_miserables_graph = nx.generators.les_miserables_graph()

nx.draw(les_miserables_graph)
plt.show()
Enter fullscreen mode Exit fullscreen mode

There are so many graphs in the generator and it's not possible for telling you about each graph in this post, so you may want to explore it yourself.


Top comments (0)