Jupyter 实验室更改 networkx 图大小

Jupyter lab change networkx graph size

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

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] 更改它时它起作用了。

你问题中的代码组成了两个数字。绘制图形的一个,然后是另一个大小为 (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') 应该将图形保存到磁盘