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

2024-04-07

NetworkX 包括功能 http://networkx.lanl.gov/reference/drawing.html用于绘制图表使用绘图库 http://matplotlib.org/。这是一个使用出色的 IPython Notebook 的示例(以ipython3 notebook --pylab inline):

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


IPython 是一个很好的工具,可以用来了解函数(和对象)的功能。如果您输入

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

you see

定义: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()

这产生

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改 networkx / matplotlib 图形绘制的属性? 的相关文章

随机推荐