0

I'm trying to save the networkx graph I'm making in python to a png file, but plt.savefig only saves a blank white png. Here's the execution line:

modeling.makeGraph(G, people)
plt.figure(3,figsize=(12,12))
modeling.draw(G, people)
plt.show()
plt.savefig("ContactTrace.png")

Here's the draw function:

def redNodes(graph, people):
    redList = []
    for node in list(graph.nodes):
        if people[node].testedPositive:
            redList.append(node)
    return redList
def blueNodes(graph, people):
    blueList = []
    for node in list(graph.nodes):
        if not people[node].testedPositive:
            blueList.append(node)
    return blueList
def draw(graph, people):
    pos = nx.spring_layout(graph)
    nx.draw_networkx_nodes(graph, pos, nodelist = redNodes(graph, people), with_labels = True, node_color = "r")
    nx.draw_networkx_nodes(graph, pos, nodelist = blueNodes(graph, people), with_labels = True)
    nx.draw_networkx_edges(graph, pos, width = 2)
    setLabels(graph, people, pos)
def setLabels(graph, people, pos):
    labels = {}
    nodes = list(graph.nodes)
    for i in range(len(nodes)):
        labels[nodes[i]] = nodes[i] + "\n" + people[nodes[i]].lastName + ",\n" + people[nodes[i]].firstName
    nx.draw_networkx_labels(graph,pos, labels,font_size = 10.5, font_weight = "bold")
    return
jackwilmer
  • 34
  • 4
  • 1
    Don't call `plt.show()` because that's rendering the plot, then `plt.savefig()` has nothing left to render. – mechanical_meat May 19 '20 at 23:11
  • 2
    Does this answer your question? [Matplotlib (pyplot) savefig outputs blank image](https://stackoverflow.com/questions/9012487/matplotlib-pyplot-savefig-outputs-blank-image) – eigenstate May 19 '20 at 23:12

0 Answers0