0

I've been trying to visualize a network I made with greater definition, and thought that saving it as a .jpg could make it easier to draw nodes apart.

edges,weights = zip(*nx.get_edge_attributes(graph_data,'weight').items())
pos = nx.kamada_kawai_layout(graph_data)
nx.draw_networkx(graph_data, pos, node_color='purple', edgelist=edges, 
                       edge_color=weights, width=5.0, edge_cmap=plt.cm.jet)
plt.savefig("filepath.png")

However, I got stumped when trying to use the plt.savefig() function. The filepath always remains empty, with just a blank picture. Am I missing something important?

rodm
  • 3
  • 2
  • Try plt.show() before plt.savefig, it seems you are not assigning anything to the figure being saved. – Celius Stingher Sep 12 '19 at 22:23
  • @CeliusStingher You should never use `plt.show()` before `plt.savefig()`, because that would save an empty figure when run as scipt in noninteractive mode (the default). – ImportanceOfBeingErnest Sep 12 '19 at 22:51
  • I expressed wrongly I meant use `plt.show()`, check what happens, if it's ok, then comment it and use `plt.savefig()`, but not both, thanks for clarifying it. Further explanation regarding the issue can be found here: https://stackoverflow.com/questions/21875356/saving-a-figure-after-invoking-pyplot-show-results-in-an-empty-file – Celius Stingher Sep 12 '19 at 22:55
  • Your commands look good to me. can you just confirm that `graph_data` is a networkx graph and that it has at least 1 node? – Joel Sep 13 '19 at 06:25
  • @Joel, yep. It's a network made with a weighted graph created elsewhere. Running it in the console outputs this: https://imgur.com/W7Zwd4w. By the way, when I try to assign the drawing itself to a variable, it comes out as a NoneType. Is that normal? – rodm Sep 13 '19 at 14:25
  • Yes - it's normal to be `None`. `draw_networkx` creates the plot, but it doesn't return anything. I don't have a good explanation for why the saved figure isn't showing the graph - especially given that it's clearly being created. Just to check - you are doing the save command without first doing `plt.show()`, right? – Joel Sep 14 '19 at 08:34
  • Yes. In fact, I'm not even doing plt.show( ). The image shows up as a result of nx.draw_networkx. – rodm Sep 14 '19 at 12:13

0 Answers0