Skip to content

Instantly share code, notes, and snippets.

@jg-you
Last active July 19, 2017 15:56
Show Gist options
  • Save jg-you/2ce69ffccf6f3ed17c09e5d7b2695f1c to your computer and use it in GitHub Desktop.
Save jg-you/2ce69ffccf6f3ed17c09e5d7b2695f1c to your computer and use it in GitHub Desktop.
Inline graph-tool figures in jupyter-notebook
# The newest version of graph-tool plots directly in a gtk window, by default.
# The following allow you to add inline plots in a jupyter-notebook (this was previously trivial).
# [The following code must appear in a notebook, obviously]
import graph_tool as gt
import graph_tool.draw
import graph_tool.collection
import matplotlib.pyplot as plt
%matplotlib inline # this allows for inline figures
graph = gt.collection.data["karate"]
plt.switch_backend('cairo') # the only supported backend
# The following should be your drawing cell:
plt.figure(figsize=(10,10))
pos = gt.draw.sfdp_layout(graph)
gt.draw.graph_draw(graph, pos=pos, mplfig=plt.gcf()) # Can add more options
@jg-you
Copy link
Author

jg-you commented Oct 18, 2016

Output:

download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment