networkx的学习笔记

这两天由于要使用python画节点图,用到networkx,记录一下用法

//
	G = nx.Graph()//生成一个图,还可以生成有向图之类的,可查阅
    G.add_edges_from(edges2)//向图G中加入边集,edges2是一个list,形似:
    //edges2=[("as","sd"),("as","df")],as,sd,df就是三个节点,加入边集后自动生成节点,不需要再加入节点集
    pos = nx.spring_layout(G)#画出来的图不同主要是因为pos方法不同,该函数是用来生成G中每个节点的位置
    nx.draw(G,pos,with_labels=True, node_size=80,edge_color='orange',node_color='white')//draw函数还有许多其他参数,此处只是引入节点位置pos,生成标签表示节点,设置节点大小,设置边颜色,设置节点颜色
    plt.show()
//这是networkx为节点生成位置的一些函数,使用这些函数就能绘制出不同的图模型
import networkx as nx
circular_layout(G[, scale, center, dim]):Position nodes on a circle.
random_layout(G[, center, dim]):Position nodes uniformly at random in the unit square.
rescale_layout(pos[, scale]):Return scaled position array to (-scale, scale) in all axes.
shell_layout(G[, nlist, scale, center, dim]):Position nodes in concentric circles.
spring_layout(G[, k, pos, fixed,]):Position nodes using Fruchterman-Reingold force-directed algorithm.
spectral_layout(G[, weight, scale, center, dim]):Position nodes using the eigenvectors of the graph Laplacian.

最近发现networkx在使用nx.draw()这个函数时,其中的参数node_color与edge_color分别都是要么只接收一个代表颜色的字符串,
要么就是接收代表颜色的字符串列表,并且,对于颜色的上色顺序,是按照你之前使用G.add_node()时,节点的加入顺序,对节点进行上色,
同理,对于边的上色也是一样,所以,如果你有需要对不同的边,点,上不同的颜色,可以利用这一点达到想要的效果
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值