0

At the moment, I am doing both:

pos = nx.spring_layout(G)

f1 = plt.figure(figsize=(18,10))

default_axes = plt.axes(frameon=True)    
nx.draw_networkx(G, node_size=600, alpha=0.8, ax=default_axes, pos=pos)

edge_labels = nx.get_edge_attributes(G, "weight")        
nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)    

plt.savefig('graph.jpg')

I would like to be able to select if to display, save or both (what I am doing now)

1 Answers1

0

There is no built-in option for that in networkx. One option is to wrap the code in a function along these lines:

def custom(G, plot=True, save_file=False):
   '''plots G by default. save_file should be a string'''
   pos = nx.spring_layout(G)

   f1 = plt.figure(figsize=(18,10))
   default_axes = plt.axes(frameon=True)    
   nx.draw_networkx(G, node_size=600, alpha=0.8, ax=default_axes, pos=pos)

   edge_labels = nx.get_edge_attributes(G, "weight")        
   nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)    

   if save: plt.savefig(save) # can allow custom save name
   if plot: plt.show()

   return

Note that if the figure displays regardless of the option passed to the command, then the inline option might need to be disabled.

SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46