23

I am drawing a graph with around 5K nodes in it using networkX and matplotlib. The GTK window by matplotlib has tools to zoom and visualise the graph. Is there any way, I can save a magnified version for proper visualisation later?

import matplotlib.pyplot as plt
import networkx as nx

pos=nx.spring_layout(G)   #G is my graph

nx.draw(G,pos,node_color='#A0CBE2',edge_color='#BB0000',width=2,edge_cmap=plt.cm.Blues,with_labels=True)
#plt.show()
plt.savefig("graph.png", dpi=500, facecolor='w', edgecolor='w',orientation='portrait', papertype=None, format=None,transparent=False, bbox_inches=None, pad_inches=0.1) 
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Nihar Sarangi
  • 4,845
  • 8
  • 27
  • 32
  • There should be a save button also.... or at least I'm sure that one of the front-ends has that (maybe Qt's?)... – dsign Feb 22 '12 at 20:22
  • yes, there is a save button. But once you magnify a portion of the diagram and save it only saves the current viewing area. I want a view of the whole graph. – Nihar Sarangi Feb 22 '12 at 20:53
  • Can't you just up the `dpi` so more detail is present in the saved image? – Hooked Feb 23 '12 at 02:37
  • @JonathonReinhart Looks fine. That import tends to be assumed ;) – tacaswell Nov 27 '13 at 05:36
  • @tcaswell I figured, I just haven't used either of these libraries much, and wanted to make sure. By the way, networkx... wow! Anyway, I'll delete my pointless comment now. – Jonathon Reinhart Nov 27 '13 at 05:56

4 Answers4

33

You have two easy options:

Up the DPI

plt.savefig("graph.png", dpi=1000)

(larger image file size)

Save as a PDF

plt.savefig("graph.pdf")

This is the best option, as the final graph is not rasterized. In theory, you should be able to zoom in indefinitely.

Hooked
  • 84,485
  • 43
  • 192
  • 261
  • 4
    @NiharSarangi I feel like the question _"save a magnified version for proper visualisation later"_ has been answered. If the image is messy and you want to customize `networkx` that might be fodder for a different question. Try turning off the labels `with_labels=True` -> `with_labels=False` in your draw command to clean it up. – Hooked Feb 23 '12 at 17:19
2

While not in GTK, you might want to check out NetworkX Viewer.

jsexauer
  • 681
  • 2
  • 12
  • 22
1

You may want to check this out:

plt.savefig("name.svg")

the quality is magnificent. although the dpi option is still alive.

Mohammad Heydari
  • 3,933
  • 2
  • 26
  • 33
0

Use this plt.savefig('name.svg') and then just upload the file to Figma -https://www.figma.com