【发布时间】:2016-09-20 08:18:20
【问题描述】:
我正在使用Networkx 使用以下代码可视化一些图表:
import networkx as nx
import matplotlib.pyplot as plt
def drawgraph(g, filename):
#plt.figure() I had to comment this line because it gives me an 'alloc: invalid block` error
nx.draw(g)
plt.draw() # I added this hoping it might solve the problem (outlined in the text below the code)
plt.savefig(filename)
#plt.show() this solves the problem, however it's blocking call and I'm drawing hundreds of graphs
现在的问题是,随后对drawgraph 的调用将导致绘制的图形与之前的图形合并,例如:如果我调用它两次,第一个绘制正确,但第二张图片包含第一个图形除了第二个图表。在函数末尾放置一个plt.show() 可以解决问题,但是这是一个阻塞调用,我不能这样做。那么我该如何解决这个问题呢?
【问题讨论】:
标签: python matplotlib networkx