我在 NetworkX 中有一个包含一些信息的图表。在展示图表后,我想将其保存为 jpg
或 png
文件。我使用了 matplotlib
函数 savefig
,但当图片保存后,它不包含任何内容。它只是一张空白的图片。
这是我写的一个样例代码:
import networkx as nx
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,12))
ax = plt.subplot(111)
ax.set_title('Graph - Shapes', fontsize=10)
G = nx.DiGraph()
G.add_node('shape1', level=1)
G.add_node('shape2', level=2)
G.add_node('shape3', level=2)
G.add_node('shape4', level=3)
G.add_edge('shape1', 'shape2')
G.add_edge('shape1', 'shape3')
G.add_edge('shape3', 'shape4')
pos = nx.spring_layout(G)
nx.draw(G, pos, node_size=1500, node_color='yellow', font_size=8, font_weight='bold')
plt.tight_layout()
plt.show()
plt.savefig("Graph.png", format="PNG")
为什么保存的图片里什么都没有(只有白色)?
这是保存的空白图片: