[英]Graph Keeps Growing, Even After Clearing It
我正在嘗試使用此Python程序繪制兩個不同的圖:
K_5=nx.complete_graph(10)
print(K_5.number_of_nodes(), K_5.number_of_edges())
nx.draw(K_5)
plt.savefig('test1.png')
K_5.clear()
G = nx.Graph()
G.add_node(8)
nx.draw(G)
plt.savefig('test2.png')
print(G.number_of_nodes(), G.number_of_edges())
結果如下圖:
[ [
我已經大量研究了Stackoverflow和matplotlib文檔,但是找不到任何有用的東西。 任何幫助將非常感激!
使用Graph.clear()
,所有已從圖形中刪除的節點和邊。 您可以在調用Graph.clear()
之后通過打印K_5.number_of_nodes()
進行檢查。 但是,在繪制第一個圖形后,您無需清除它,因此,它會繪制在第一個圖形的頂部。
因此,您需要清除matplotlib的當前數據。 您可以使用plt.clf()
。
import networkx as nx
import matplotlib.pyplot as plt
K_5=nx.complete_graph(10)
print(K_5.number_of_nodes(), K_5.number_of_edges())
nx.draw(K_5)
plt.savefig('test1.png')
K_5.clear()
plt.clf() # new line, to clear the old drawings
G = nx.Graph()
G.add_node(8)
nx.draw(G)
plt.savefig('test2.png')
print(G.number_of_nodes(), G.number_of_edges())
test1.png:
test2.png:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.