Matplotlib不显示最后一个figu

2024-05-14 23:18:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用matplotlib来显示图形,但是当涉及到最后一个图形时,它不显示其内容(只是创建窗口)。 我用它来刻画图的最大独立集。所以,集合实际上是一个包含节点列表的列表。你知道吗

pos=nx.spring_layout(G) # positions for all nodes

for i in range(0, len(sets)):

    nx.draw_networkx_nodes(G,pos, sets[i],
        node_color='r', node_size=500,  alpha=0.8)

    rest = []
    for j in nodes:
            if j not in sets[i]:
                rest.append(j)

    nx.draw_networkx_nodes(G,pos, rest,
        node_color='w', node_size=500,  alpha=0.8)

    nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5)


    plt.axis('off')
    plt.figure(i)

plt.show()

Tags: inposalphanetworkxrestnode图形列表
1条回答
网友
1楼 · 发布于 2024-05-14 23:18:23

试试这个:

pos=nx.spring_layout(G) # positions for all nodes

for i in range(0, len(sets)):

    nx.draw_networkx_nodes(G,pos, sets[i],
    node_color='r', node_size=500,  alpha=0.8)

    rest = []
    for j in nodes:
            if j not in sets[i]:
                rest.append(j)

    nx.draw_networkx_nodes(G,pos, rest,
        node_color='w', node_size=500,  alpha=0.8)

    nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5)


    plt.axis('off')
    plt.show(block=False)

plt.show()移动到循环中并移除plt.figure(i) 这样就不会看到空的绘图,也不需要手动关闭绘图。你知道吗

相关问题 更多 >

    热门问题