【问题标题】:In Networkx is there a way to select when to save to a file, when show on screen and when both?在 Networkx 中,有没有一种方法可以选择何时保存到文件、何时显示在屏幕上以及何时同时保存?
【发布时间】:2022-11-01 11:52:57
【问题描述】:

目前,我正在做这两件事:

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')

我希望能够选择是否显示、保存或两者兼而有之(我现在正在做什么)

【问题讨论】:

    标签: save networkx display


    【解决方案1】:

    networkx 中没有内置选项。一种选择是按照以下方式将代码包装在函数中:

    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
    

    请注意,如果无论传递给命令的选项如何都显示该图形,则inline option might need to be disabled

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      相关资源
      最近更新 更多