gpt4 book ai didi

python - 如何更改 networkx/matplotlib 图形绘图的属性?

转载 作者:太空狗 更新时间:2023-10-29 21:29:35 24 4
gpt4 key购买 nike

NetworkX 包括 functions使用 matplotlib 绘制图形.这是一个使用出色的 IPython Notebook 的示例(从 ipython3 notebook --pylab inline 开始):

enter image description here

很好,作为一个开始。但是我怎样才能影响绘图的属性,比如着色、线宽和标签?我以前没有使用过 matplotlib。

最佳答案

IPython 是找出函数(和对象)可以做什么的好工具。如果你输入

[1]: import networkx as nx
[2]: nx.draw?

你看

Definition: nx.draw(G, pos=None, ax=None, hold=None, **kwds)

**kwds: optional keywords
See networkx.draw_networkx() for a description of optional keywords.

如果你因此输入

[10]: nx.draw_networkx?

你会看到

node_color: color string, or array of floats
edge_color: color string, or array of floats
width: float
Line width of edges (default =1.0)
labels: dictionary
Node labels in a dictionary keyed by node of text labels (default=None)

因此,有了这些信息和一些实验,不难得出:

import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
import string

G = nx.generators.erdos_renyi_graph(18, 0.2)
nx.draw(G,
node_color = np.linspace(0,1,len(G.nodes())),
edge_color = np.linspace(0,1,len(G.edges())),
width = 3.0,
labels = {n:l for n,l in zip(G.nodes(),string.ascii_uppercase)}
)
plt.show()

产生

enter image description here

关于python - 如何更改 networkx/matplotlib 图形绘图的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com