Jupyter Lab更改networkx图的大小

3

我有一个 Jupyter Notebook,想在 Jupyter Lab 中打开。我的代码是:

import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
mylist = ["a", "b", "c", "d"]
G.add_nodes_from(mylist)
nx.draw(G)
plt.figure(3,figsize=(100,100))

然而,更改figsize并不会改变输出。如何在Jupyter Lab中做到这一点?我尝试了保存图表,但是当我使用plt.figure()时只保存了一个白色页面。

解决方法: 如果有人想知道同样的事情:当我使用plt.rcParams['figure.figsize'] = [10, 50]进行更改时,它可以正常工作。


它正常运行得非常完美。 - Ajay
@Ajay,在我的笔记本电脑上没有出现这种情况。它显示的是“图像尺寸为72000x36000",这些值会变化,但是单元格的大小不会改变,节点之间的间距也不会比以前更大。 - LizzAlice
被接受的答案在Jupyter Notebook中对我不起作用,但您脚注中的解决方案有效。我建议将脚注添加为答案。 - Chaos
1个回答

3

你的问题中的代码绘制了两个图形。一个是用于绘制图形的,另一个大小为(100,100)。你在绘制图形之后定义了第二个图形,所以,如果你调用plt.savefig()函数,当前(空)图形将保存到磁盘。

重新组织你的代码:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
mylist = ["a", "b", "c", "d"]
G.add_nodes_from(mylist)

fig, ax = plt.subplots(figsize=(10,10)) # i am suggesting (10,10) or something in that neighbourhood, 
                                        # because the numbers are inches. So (100,100) will give you 
                                        # a figure of size (100 inches by 100 inches)

nx.draw(G, ax=ax) # to ensure the graph is drawn on the appropriate part of the figure

现在,调用plt.savefig('test123.png')会将图保存到磁盘上。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接