我的代码如下:
import ConfigParser
import sys
import time
import matplotlib.pyplot as plt
import networkx as nx
import json
from networkx.algorithms import bipartite
def create_graph(senators):
G= nx.Graph()
G.add_nodes_from(senators)
return G
senators=["ab","cd","ef"]
graph = create_graph(senators)
nx.draw(graph,with_labels=True)
plt.savefig("p1.png")
graph.clear()
graph = nx.DiGraph()
print graph.nodes()
nx.draw(graph,with_labels=True)
plt.savefig("p2.png")在我的代码中,我尝试绘制两个图片:p1.png和p2.png。在我绘制p1.png之后,我清除了graph。但是,p2.png与p1.png具有相同的节点。
我不知道我的代码出了什么问题。因为我有清晰的图形,所以在p2.png中应该没有任何内容
有什么问题吗?
发布于 2014-09-06 20:12:59
“清算”有两个不同的概念。一种是从图中删除节点和边(graph.clear),另一种是擦除图形(plt.clf())。您将删除节点和边,但不会清除地物轴。因此,您在p2.png中保存的只是原始的p1.png图形。尝试在第二个nx.draw()之前添加一个plt.clf()。
https://stackoverflow.com/questions/25695901
复制相似问题