Generate graphe with python

#python

Draw a graph yes, but save as image

OK so, Graph are cool. They are beautiful, insightful and everywhere.

Anyway, with networkx and pyplot, you can easily plot a graph:

import networkx as nx
import matplotlib.pyplot as plt


def showgraph():
    G = nx.Graph()
    edges = []

    edges.append(["Python", "networkx"])
    edges.append(["Python", "pyplot"])
    edges.append(["Python", "pandas"])
    edges.append(["Python", "C"])
    edges.append(["C", "Segfault"])
    edges.append(["C", "gcc"])
    G.add_edges_from(edges)
    nx.draw_networkx(G)
    plt.show()

Sweet, you get pyplot running and showing a graph !

Cool but what if you want to integrate this graph into an article in LaTeX or in a blog post, then you kinda want your script to directly generate an image.

    # replace plt.show with plt.savefig
    # plt.show()
    plt.savefig("./example-graphe.png")
$ python main.py

…and there you go:

example graphe