Jupyter lab change networkx graph size

Viewed 1151

I have a Jupyter Notebook I am opening in Jupyter Lab. My code is:

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))

However, changing the figsize does not change the output. How can this be done in Jupyter Lab? I tried saving the Graph instead, but when I use plt.figure() just a white page is saved.

Solution: In case anyone is wondering the same thing: When I changed it with plt.rcParams['figure.figsize'] = [10, 50] it worked.

1 Answers

The code in your question makes two figures. One onto which the graph is drawn, and then another one of size (100,100). You define this second figure after you have already drawn your graph, so, if you call plt.savefig(), this current (empty) figure is saved to disk.

Reorganising your code:

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

Now, calling plt.savefig('test123.png') should save the figure to disk

Related