networkx:在draw_circular中更改节点颜色 [英] networkx: change node color in 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屋!