Python matplotlib:图例不从networkx图形继承节点颜色
我在Windows7上使用Python 2.7运行matplotlib v1.4.3。当我使用networkx生成图形时,通过node_color参数传递的节点颜色不会被图例继承。图例的圆圈是默认的蓝色。下面的代码是显示问题的测试用例。我是SO的新用户,无法发布图像,但您可以复制/粘贴代码以查看问题Python matplotlib:图例不从networkx图形继承节点颜色,python,matplotlib,networkx,Python,Matplotlib,Networkx,我在Windows7上使用Python 2.7运行matplotlib v1.4.3。当我使用networkx生成图形时,通过node_color参数传递的节点颜色不会被图例继承。图例的圆圈是默认的蓝色。下面的代码是显示问题的测试用例。我是SO的新用户,无法发布图像,但您可以复制/粘贴代码以查看问题 import networkx as nx import matplotlib.pyplot as plt import numpy as np # define nodes, node type
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
# define nodes, node types, and color range
np.random.seed(10000)
nodes = list('abcdefghijkl')
nodeTypes = ['foo','bar','baz']
nodeColors = [0.0, 0.5, 1.0]
# assign each node a type and color via a dictionaries
nodeTypeDict = dict(zip(nodeTypes, [nodes[:4],nodes[4:8],nodes[8:]]))
nodeColorDict = dict(zip(nodeTypes, nodeColors))
nodePos = dict(zip(nodes,[(np.random.random(),np.random.random())
for i in range(len(nodes))]))
# generate the graph
g = nx.Graph()
g.add_nodes_from(nodes)
# create image canvas and axes
fig, ax = plt.subplots(1, figsize=(6,6))
# iterate each nodetype, changing colors and labels of the nodes
for nt in nodeTypes:
# choose nodes and color for each iteration
nlist = nodeTypeDict[nt]
ncolor = [nodeColorDict[nt]]*len(nlist)
# draw the graph
nx.draw_networkx_nodes(g,
pos=nodePos,
nodelist=nlist,
ax=ax,
cmap=plt.cm.brg,
vmin=0.0,
vmax=1.0,
node_color=ncolor,
label=nt) # the label for each iteration is
# the node type
# here is the problem. The legend does not inherit the colors.
ax.legend(scatterpoints=1)
plt.show()
这是一个有效的版本
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
# define nodes, node types, and color range
np.random.seed(10000)
nodes = list('abcdefghijkl')
nodeTypes = ['foo','bar','baz']
nodeColors = ['r', 'b', 'k']
# assign each node a type and color via a dictionaries
nodeTypeDict = dict(zip(nodeTypes, [nodes[:4],nodes[4:8],nodes[8:]]))
nodeColorDict = dict(zip(nodeTypes, nodeColors))
nodePos = dict(zip(nodes,[(np.random.random(),np.random.random())
for i in range(len(nodes))]))
# generate the graph
g = nx.Graph()
g.add_nodes_from(nodes)
# create image canvas and axes
fig, ax = plt.subplots(1, figsize=(6,6))
# iterate each nodetype, changing colors and labels of the nodes
for nt in nodeTypes:
# choose nodes and color for each iteration
nlist = nodeTypeDict[nt]
ncolor = nodeColorDict[nt]
print ncolor
# draw the graph
nx.draw_networkx_nodes(g,
pos=nodePos,
nodelist=nlist,
ax=ax,
node_color=ncolor,
label=nt) # the label for each iteration is
# the node type
# here is the problem. The legend does not inherit the colors.
ax.legend(scatterpoints=1)
plt.savefig('tmp.png')
我以为我能很快解释出什么地方出了问题,但我真的不知道。首先-如果要使特定命令的所有颜色相同,则无需将颜色列表发送到draw\u networkx\u节点
。把它换成单色就行了。我原以为这样可以解决问题,但后来我在cmap上遇到了麻烦。然后我明确说明了使用什么颜色,问题就解决了
所以-我得到的效果很好,当它绘制图例时,它似乎没有得到颜色映射编码的正确颜色。我建议在调用
draw\u networkx\u nodes
之前先确定颜色,然后发送颜色即可。谢谢。这很有帮助。看起来color_节点只希望来自cmap的RGB而不是RGBa能够传递到图例。我在nodeColorsIndex中为nci添加了nodeColorsIndex=[0.0,0.5,1.0]
和nodeColors=[colmap(nci)[0:3]
,效果非常好。是的,我只是在玩它,发现问题是它需要长度为3。我仍然很难弄清楚如何让它尊重vmin和vmax参数。如果对你有用的话,我现在就把它留下。