如何为Networkx+;python中生成.png文件的Matplotlib
作为标题,每当我使用Pyinstaller或py2exe编译成.exe文件时,它都会失败 我已经尝试了这个简单的代码,作为示例 () 它在Jupyter笔记本上运行良好,并使用cmd如何为Networkx+;python中生成.png文件的Matplotlib,python,matplotlib,networkx,Python,Matplotlib,Networkx,作为标题,每当我使用Pyinstaller或py2exe编译成.exe文件时,它都会失败 我已经尝试了这个简单的代码,作为示例 () 它在Jupyter笔记本上运行良好,并使用cmdpython-graph.py 但它从未在编译的.exe文件上运行 Pyinstaller的可执行文件崩溃 Py2exe的可执行文件在第一次打开时闪过错误消息,它立即关闭,所以我不知道是什么错误 我的networkx版本:2.5、matplotlib:3.2.2、python 3.8.3、最新的pyinstall
python-graph.py
但它从未在编译的.exe文件上运行
- Pyinstaller的可执行文件崩溃
- Py2exe的可执行文件在第一次打开时闪过错误消息,它立即关闭,所以我不知道是什么错误
提前感谢您是否尝试删除
plt.show()
?如果您只想保存图像,则不需要显示图像,因为图形窗口等可能会导致一些问题。@Sparky05是的,我已将其删除,没有运行。您是否尝试删除plt.show()
?如果您只想保存图像,则无需显示,因为图形窗口等可能会导致一些问题。@Sparky05是的,我已将其删除,未运行
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.savefig("Graph.png", format="PNG")
plt.show()