Display graph without saving using pydot

user506710 picture user506710 · Jan 4, 2011 · Viewed 24.8k times · Source

I am trying to display a simple graph using pydot.

My question is that is there any way to display the graph without writing it to a file as currently I use write function to first draw and then have to use the Image module to show the files.

However is there any way that the graph directly gets printed on the screen without being saved ??


Also as an update I would like to ask in this same question that I observe that while the image gets saved very quickly when I use the show command of the Image module it takes noticeable time for the image to be seen .... Also sometimes I get the error that the image could'nt be opened because it was either deleted or saved in unavailable location which is not correct as I am saving it at my Desktop..... Does anyone know what's happening and is there a faster way to get the image loaded.....

Thanks a lot....

Answer

micahscopes picture micahscopes · Apr 7, 2016

Here's a simple solution using IPython:

from IPython.display import Image, display

def view_pydot(pdot):
    plt = Image(pdot.create_png())
    display(plt)

Example usage:

import networkx as nx
to_pdot = nx.drawing.nx_pydot.to_pydot
pdot = to_pdot(nx.complete_graph(5))
view_pydot(pdot)