如何解决Jupyter 实验室更改 networkx 图形大小
我有一个 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]
更改它时,它起作用了。
解决方法
您问题中的代码有两个数字。在其上绘制图形的一个,然后另一个大小为 (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')
应该将图形保存到磁盘
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。