如何更改networkx/matplotlib图形图的属性? [英] How to change attributes of a networkx / matplotlib graph drawing?

查看:253
本文介绍了如何更改networkx/matplotlib图形图的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NetworkX包括功能,用于使用

NetworkX includes functions for drawing a graph using matplotlib. This is an example using the great IPython Notebook (started with ipython3 notebook --pylab inline):

很好,首先.但是如何影响图形的属性,例如颜色,线宽和标签?我以前从未使用过matplotlib.

Nice, for a start. But how can I influence attributes of the drawing, like coloring, line width and labelling? I have not worked with matplotlib before.

推荐答案

IPython是一个很好的工具,用于找出函数(和对象)可以做什么.如果您输入

IPython is a great tool for finding out what functions (and objects) can do. If you type

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

你看到

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

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)

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

So, armed with this information, and a bit of experimentation, it is not hard to arrive at:

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

产生

这篇关于如何更改networkx/matplotlib图形图的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆