0

I'm trying to iteratively save about 1000 images. Each image is a picture of a graph from networkx. The problem is that after about 500 images my computer crashes from memory overload.

The loop looks something like:

for subgraph in chosen_subgraphs:
        plt.figure(figsize=(10,10))
        pos = nx.spring_layout(subgraph) 
        nx.draw(subgraph, pos, edge_color='black', width=2, linewidths=1,
            node_size=1500, node_color='pink', alpha=0.9, font_size=10,
            labels={node: node for node in subgraph.nodes()})

        nx.draw_networkx_edge_labels(
            subgraph, pos,
            edge_labels = labels,
            font_color='red', font_size=10
        )
        # save network picture
        plt.savefig(f'st/{k}/{counter}/network_img.png')
        plt.close('all')

I found this SO question that talks about how to close a matplotlib image which I followed with plt.close('all'), but it didn't seem to work. I also tried using plt.clf() right before it, but it also didn't work.

Penguin
  • 1,923
  • 3
  • 21
  • 51

0 Answers0