Python networkx 图:不要将旧图与新图一起绘制 [英] Python networkx graph: Do not draw old graph together with new graph

查看:56
本文介绍了Python networkx 图:不要将旧图与新图一起绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码:

import networkx as nx
for i in range(2):
    G = nx.DiGraph()
    if i==0:
        G.add_edge("A", "B")
    elif i==1:
        G.add_edge("A", "C")
    import matplotlib.pyplot as plt
    nx.draw(G)
    plt.savefig(str(i)+".png")
    G.clear()

它应该在文件 0.png 中画线 AB,在文件 1.png 中画线 AC.但是,在我运行它之后.在0.png中,有一行AB,但在1.png中,有两行:AB和AC.虽然我有G.clear()",但似乎没有清理 0.png 的内存.

It should draw line AB in file 0.png and draw line AC in file 1.png. But, after I ran it. In 0.png, there is one line AB, but in 1.png, there are two lines: AB and AC. It seems that the memory for 0.png is not cleaned, although I have had "G.clear()".

有人知道怎么解决吗?

推荐答案

我有解决方案.

plt.savefig(str(i) + ".png")之后添加plt.clf().它可以清理pyplot中的旧图形.我希望它可以帮助某人.

Add plt.clf() after plt.savefig(str(i) + ".png"). It can clean the old graph in pyplot. I hope it can help someone.

这篇关于Python networkx 图:不要将旧图与新图一起绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆