如何在networkx中修改节点的轮廓颜色? [英] How can one modify the outline color of a node In networkx?

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

问题描述

我对networkx和使用matplotlib.pyplot进行绘制相对较新,并且想知道如何修改节点轮廓的颜色(或其他属性,例如权重). 轮廓"不是指两个节点之间的弧线或边线;我的意思是,围绕圆的黑色细线在绘制网络时默认用于表示节点.

I am relatively new to networkx and plotting using matplotlib.pyplot and would like to know how to modify the color (or other attributes such as weight) of a node's outline. By "outline" I don't mean an arc or edge between two nodes; I mean the thin black line around the circle that is by default used to represent a node when plotting a network.

例如,当我制作一个只有一个节点的图并显示它时:

So for example, when I make a graph that has just a single node and display it:

from networkx import *
import matplotlib.pyplot as plt
plt.ion()
G = Graph()
G.add_node(1)
draw(G)

我看到一个红色的节点,它的黑色轮廓很细(里面是黑色的"1",这是该节点的标签).如何将轮廓的颜色从黑色更改为红色(#FF0000")或其他颜色?或者,我可以完全取消显示轮廓吗?

I see a single red node with a thin black outline (and a black "1" inside it, which is the node's label). How can I change the color of that outline from black to, say, red ("#FF0000"), or another color? Alternatively, can I suppress the display of the outline entirely?

我在想必须有一个可以设置的类似于edge_color或node_color的属性.但是,我无法通过网络搜索,networkx文档或查看图库示例来找到这样的属性.如果有人可以将我指向适当的属性,将不胜感激.谢谢!

I'm imagining there must be an attribute, analogous to edge_color or node_color, that I can set. However, I was not able to find such an attribute via a web search, in the networkx documentation, or by looking at gallery examples. If someone can point me to the appropriate attribute(s) that would be greatly appreciated. Thanks!

推荐答案

更新(3/2019):

UPDATE (3/2019): as of networkx 2.1, the kwargs are forwarded from draw(), so you should be able to simply call draw() with the edge_color kwarg.

好吧,这有点hacky,但是可以用.这就是我的想法.

Ok, this is kind of hacky, but it works. Here's what I came up with.

问题

networkx.draw()调用networkx.draw_networkx_nodes(),然后调用pyplot.scatter()绘制节点.问题是draw_networkx_nodes()接受的关键字args不会传递给scatter(). (此处来源)

networkx.draw() calls networkx.draw_networkx_nodes(), which then calls pyplot.scatter() to draw the nodes. The problem is that the keyword args accepted by draw_networkx_nodes() aren't passed on to scatter(). (source here)

为解决此问题,我基本上将networkx.draw()分解为以下组件:draw_networkx_nodesdraw_networkx_edgesdraw_networkx_labels.

To solve this, I basically broke apart networkx.draw() into its components: draw_networkx_nodes, draw_networkx_edges, and draw_networkx_labels.

解决方案

我们可以将draw_networkx_nodes()的返回值-一个PathCollection-并对其进行操作:您可以分别将PathCollection.set_edgecolor()PathCollection.set_edgecolors()与颜色或列表一起使用.

We can take the return value of draw_networkx_nodes() -- a PathCollection -- and operate on that: you can use PathCollection.set_edgecolor() or PathCollection.set_edgecolors() with either a color or a list, respectively.

示例代码:

from networkx import *
import matplotlib.pyplot as plt
G = Graph()
G.add_node(1)
# Need to specify a layout when calling the draw functions below
# spring_layout is the default layout used within networkx (e.g. by `draw`)
pos = spring_layout(G)
nodes = draw_networkx_nodes(G, pos)
# Set edge color to red
nodes.set_edgecolor('r')
draw_networkx_edges(G, pos)
# Uncomment this if you want your labels
## draw_networkx_labels(G, pos)
plt.show()

如果您将经常使用此功能,则重新定义draw_networkx_nodes以便将kwarg传递给scatter可能更有意义(IMO).但是上面的可以起作用.

If you're going to be using this a lot, it probably makes more sense (IMO) to just redefine draw_networkx_nodes to actually pass the kwargs to scatter. But the above will work.

要完全去除标记边缘,只需将颜色设置为None而不是'r'.

To remove the marker edges entirely, simply set the color to None instead of 'r'.

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

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