networkx:在draw_circular中更改节点颜色 [英] networkx: change node color in draw_circular

查看:461
本文介绍了networkx:在draw_circular中更改节点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用networkx和draw_circular绘制图形

I draw a graph with networkx and draw_circular

  networkx.draw_circular(g)

我尝试更改某些节点的颜色,也许使用

I try change the color of some nodes, maybe with draw_networkx_nodes.
but for this, I need know the node position, how I can get the position of nodes in draw_circular ?
or directly, how I can change the color of some nodes in draw_circular?

推荐答案

draw_circular将接受与 matplotlib 可以识别的任何东西.

draw_circular will accept keyword arguments same as draw_networkx. There is an optional argument node_color where you can supply colors for individual nodes. Argument passed to node_color must be a list with the length as number of nodes or a single value that will be used for all nodes. Color can be anything that is recognized by matplotlib.

所以类似这样的结果如下:

So something like this would give the result below:

import networkx as nx
import matplotlib.pyplot as plt
from random import random
g = nx.random_graphs.erdos_renyi_graph(10,0.5)
colors = [(random(), random(), random()) for _i in range(10)]
nx.draw_circular(g, node_color=colors)
plt.show()

修改

从视觉上讲,您可以使用 networkx.layout.circular_layout 等.

Optinoally, you can get the positions of nodes for certain layout with the networkx.layout.circular_layout, etc..

这篇关于networkx:在draw_circular中更改节点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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