使用NetworkX绘制彩色的树 [英] Drawing colored trees with NetworkX
问题描述
注意-这是对上一个问题的完整重写,我认为这太复杂了.它提供了一个简单得多的版本,如果我能解决的话,将会取得进展.
NOTE -- This is a complete rewrite of the previous question, which I think was too complex. It presents a much simpler version which, if I can solve, will result in progress.
我已经从以下SO答案中修改了我的代码:有没有办法保证NetworkX的分层输出?
I have adapted my code from this SO answer: Is there a way to guarantee hierarchical output from NetworkX?
通过以下非常简单的代码,我得到了奇怪的着色行为.
With the following very simple code, I get strange coloring behavior.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
G.add_node("ROOT")
for i in xrange(1):
for j in range(1):
G.add_node(str(j)+"_%i" % i, )
if j ==0:
G.add_edge("ROOT", str(j)+"_%i" % i)
else:
G.add_edge(str(j-1)+"_%i" %i, str(j)+"_%i" % i)
pos=nx.graphviz_layout(G,prog='dot')
for i in xrange(1):
nodelist = ['ROOT']
for j in range(1):
nodelist.append(str(j)+"_%i" % i )
nx.draw_networkx_nodes(G,pos, nodelist=nodelist, cmap=plt.get_cmap('Set3'), node_color=[0,1])
nx.draw_networkx_edges(G, pos,arrows=True)
limits=plt.axis('off')
我给node_color
赋什么值似乎无关紧要,仅取决于这些值是否不同.例如,使用node_color = [0,1]
时,我得到的行为与[0,.1]
或[0,1000]
时完全相同. (为什么?颜色图的取值在0到1之间).
It seems to NOT matter what values I give for the node_color
, only whether the values are different or not. For example, with node_color = [0,1]
, I get the exact same behavior as when it is [0,.1]
, or [0,1000]
. (Why? The colormap takes values between 0 and 1).
但是,如果我更改颜色图,则颜色也会更改.例如:
However if I change the colormap, the colors DO change. For example:
如果我设置node_color = [3,3]
(或任何两个相同的值),我总是得到相同的东西,节点的颜色相同.
And if I set node_color = [3,3]
(or any two values which are the same), I always get the same thing, the nodes are colored identically.
有什么想法我在这里做错了吗?
Any ideas what I'm doing wrong here?
推荐答案
在进入颜色图之前,将节点颜色值标准化为间隔[0,1].据推测,这意味着要使用整个颜色范围,而与给定值的范围无关.要使用不同的缩放间隔,可以设置vmin
和vmax
参数:
Before getting to the colormap, the node color values are normalized to the interval [0, 1]. This is presumably meant to use the full range of colors, regardless of the range of the values given. To use a different interval for scaling, you can set the vmin
and vmax
parameters:
nx.draw_networkx_nodes(G,pos, nodelist, cmap=plt.get_cmap('Set3'),
node_color=[0,1], vmin=0, vmax=100)
详细说明:
A detailed explanation:
根据
From the description of the node_color parameter in the draw_networkx_nodes() docs:
如果是数值 指定它们将使用cmap和 vmin,vmax参数.有关更多详细信息,请参见matplotlib.scatter.
If numeric values are specified they will be mapped to colors using the cmap and vmin,vmax parameters. See matplotlib.scatter for more details.
不幸的是,这些文档不能很好地描述vmin
和vmax
的行为.但是对matplotlib.scatter的引用确实更详细地介绍了它: >
Unfortunately, these docs don't do a good job of describing the behaviour of vmin
and vmax
. But the reference for matplotlib.scatter does cover it more thouroughly:
vmin和vmax与范数结合使用以对亮度数据进行归一化. 如果任何一个都不为,则使用颜色阵列的最小值和最大值.
vmin and vmax are used in conjunction with norm to normalize luminance data. If either are None, the min and max of the color array is used.
因此,默认情况下,所传递的node_color数组的最小值在颜色图上映射为0,最大值为1.使用线性映射将介于两者之间的所有内容映射到间隔[0.0,1.0](此方法被称为 normalization 或 功能缩放 ) .以下是一些示例,从node_color
到色图域中的点:
So, the minimum of the node_color array you pass is, by default, mapped to 0 on the colormap, and the maximum to 1. Everything in between is mapped to the interval [0.0,1.0] using a linear mapping (this is called normalization or feature scaling). Here are some examples, going from node_color
to points in the colormap's domain:
- [0,0.1]→[0,1]
- [0,1000]→[0,1]
- [0,200,1000]→[0,0.2,1]
- [10、15、20]→[0、0.5、1]
- [0,0]→[0,0] (或者可能是[1,1];我实际上没有运行它)
- [0, 0.1] → [0, 1]
- [0, 1000] → [0, 1]
- [0, 200, 1000] → [0, 0.2, 1]
- [10, 15, 20] → [0, 0.5, 1]
- [0, 0] → [0,0] (or it could be [1,1]; I didn't actually run this)
这篇关于使用NetworkX绘制彩色的树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!